Domanda

I am trying to concat two directory listings into one array, I get the directory arrays to show the list of files in each separately (NSLog), but concatenating them together via addObjectsFromArray, results in (null):

NSString *path1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/Dir1/"];
NSString *path2 = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/Dir2/"];

NSArray *directoryList1 = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path1 error:nil]
                                  pathsMatchingExtensions:[NSArray arrayWithObjects:@"log", nil]];

NSArray *directoryList2 = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path2 error:nil]
                                  pathsMatchingExtensions:[NSArray arrayWithObjects:@"log", nil]];

NSLog(@"directoryList1 contains: %@", directoryList1);
NSLog(@"directoryList2 contains: %@", directoryList2);

NSMutableArray *directoryList;
[directoryList addObjectsFromArray:directoryList1];
[directoryList addObjectsFromArray:directoryList2];

NSLog(@"directoryList contains: %@", directoryList);
È stato utile?

Soluzione

This:

NSMutableArray *directoryList;

defines a pointer to an array but doesn't create an instance. You should have:

NSMutableArray *directoryList = [NSMutableArray array];

so you have something to add the contents to.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top