在我的应用程序中,我将从手动创建对象数组转变为创建一个相等的数组,其中对象存储在引用文件夹中。我用contentsOfDirectoryAtPath创建了自动数组。
问题是当我稍后在我的集合视图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以供参考
-(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;
}发布于 2014-10-06 06:51:29
如果您知道"contentsOfDirectoryAtPath“方法总是在单个元素外部数组中返回一个对象数组,那么您可以通过以下方式”解包“内部数组:
NSArray *targetArray = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:nil] objectAtIndex:0];(我没有在我的计算机上通过XCode运行上面的代码来检查它的编译情况)
https://stackoverflow.com/questions/26207832
复制相似问题