在我的应用程序中,我将手动创建一个对象数组,以创建一个平等数组,其中的对象存储在参考文件夹中。我用contentsOfDirectoryAtPath创建自动数组。手动VS自动数组澄清
问题是当我在集合视图cellForItemAtIndexPath中稍后引用数组时。手动创建的数组运行正常,但自动数组的格式不相同并引发错误。
不同之处在于手动创建的数组有3个对象,自动创建的数组有一个对象,在日志中由额外的括号显示。我如何获得自动创建的数组来添加每个文件夹对象,这样我就可以像使用手动创建的数组一样获得一个包含3个对象的数组?在此先感谢您的时间。
手动创建targetArray
//@interface
@property (nonatomic, strong) NSArray *targetArray;
//viewdidLoad
self.targetArray = @[@"one",@"two", @"three"];
NSLog(@"Target Array description test = %@", [_targetArray description]);
//returns
Target Array description test = (
"one",
"two",
"three"
)
通过阅读参考文件夹
//@interface
@property (nonatomic, strong) NSArray *targetArray;
//viewdidLoad
for (int i = 0; i <= [folderArray count] -1; i++) {
NSString *sectionString = [folderArray objectAtIndex:i];
NSMutableArray *subFolderArray = [[NSMutableArray alloc] init];
NSArray *targetArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:nil];
NSLog(@"Target Array description test = %@", [_targetArray description]);
}
//returns
Target Array description test = (
(
"one",
"two",
"three"
)
)
的cellForRowAtIndexPath为W /错误日志以供参考
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
sheetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SheetCell" forIndexPath:indexPath];
NSLog(@"Target Array description test = %@", [_targetArray description]);
NSString *sheetString = [self.targetArray objectAtIndex:indexPath.row];
ERROR: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM stringByDeletingPathExtension]: unrecognized selector sent to instance 0x78794450'
cell.sheetImageView.image = [UIImage imageNamed:sheetString];
cell.sheetLabel.text = sheetString;
return cell;
}
您似乎有两个不同的对象,名为targetArray:一个是self.targetArray(也可以通过其实例变量_targetArray访问),另一个是在for循环中声明的另一个对象(NSArray * targetArray)。您的NSLog只能引用前者。你的for循环也没有关闭} - 是一个错字? – pbasdf 2014-10-05 22:41:06