连接两个数组会导致null

时间:2014-01-16 20:26:26

标签: objective-c nsmutablearray nsarray nsfilemanager

我试图将两个目录列表连接成一个数组,我得到目录数组以分别显示每个文件的列表(NSLog),但是通过addObjectsFromArray将它们连接在一起,结果为(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);

1 个答案:

答案 0 :(得分:0)

此:

NSMutableArray *directoryList;

定义指向数组的指针,但不创建实例。你应该:

NSMutableArray *directoryList = [NSMutableArray array];

所以你有东西可以添加内容。