2012-01-19 32 views
0

我试图从文件路径组件创建一个数组。我的文件路径的数组(如NSString的),我通过他们循环,再破每下跌,像这样:Objective-C:从NSString制作NSArray

//get the array of image paths 
imageList = [[notification userInfo] objectForKey:@"images"]; 

//loop through the array and get the image names 
for (NSString* thisImagePath in imageList) { 
    NSArray* thisImagePathArray = [thisImagePath componentsSeparatedByString:@"/"]; 

可以说,我计划在这里崩溃的时间。我收到以下错误消息:

-[NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x1a7940 

imageList是放入视图的文件数组。因为这个问题开始出现,我一次只删除一个文件。例如:

这个文件没有工作:

/Users/steve/Desktop/thinkstock/PT121211_PI_bariatric.tif 

这确实

/Users/steve/Desktop/thinkstock/Studentexhausted82557038.jpg 

所以,如果我理解正确的错误消息,我想给componentsSeparatedByString选择应用到一个NSArray,不支持该选择器。但在我的循环中,我正在调用NSString,如果对象是一个数组,我不应该在那里崩溃? (并且我99%确定imageList的索引0处的对象是一个字符串。)

我的目标是从文件路径获取文件名,有没有比我采用的方法更好的方法?

当我通过(把一个调试点似乎工作,我策划了componentsSeparatedByString线步骤:

enter image description here

但如果我打继续它崩溃

至于建议我改变。我的代码来记录数据:

//loop through the array and get the image names 
for (NSString* thisImagePath in imageList) { 
if (![thisImagePath isKindOfClass:[NSString class]]) { 
    NSLog(@"The class of this object is: %@", [thisImagePath className]); 
} 
NSLog(@"%@", thisImagePath); 

NSArray* thisImagePathArray = [thisImagePath componentsSeparatedByString:@"/"]; 
NSString* thisImageName = [thisImagePathArray objectAtIndex:[thisImagePathArray count]-1]; 

类检查的条件永远不会被触发,因为一切都是NSS特级。然而,一些文件的工作,一些不...

2012-01-19 13:59:04.631 archiveDrop_cocoa[76758:10b] /Users/steve/Desktop/thinkstock/rbrb_0556.jpg 
2012-01-19 13:59:06.799 archiveDrop_cocoa[76758:10b] /Users/steve/Desktop/thinkstock/Manracefinish78464007.jpg 
2012-01-19 13:59:08.319 archiveDrop_cocoa[76758:10b] /Users/steve/Desktop/thinkstock/ManLabtop86510699.jpg 
2012-01-19 13:59:08.320 archiveDrop_cocoa[76758:10b] *** -[NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x1a75c0 
2012-01-19 13:59:08.321 archiveDrop_cocoa[76758:10b] *** Canceling drag because exception 'NSInvalidArgumentException' (reason '*** -[NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x1a75c0') was raised during a dragging session 
2012-01-19 13:59:10.726 archiveDrop_cocoa[76758:10b] /Users/steve/Desktop/thinkstock/LasVegassign78058995.jpg 
2012-01-19 13:59:10.728 archiveDrop_cocoa[76758:10b] *** -[NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x1a9010 
2012-01-19 13:59:10.729 archiveDrop_cocoa[76758:10b] *** Canceling drag because exception 'NSInvalidArgumentException' (reason '*** -[NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x1a9010') was raised during a dragging session 
2012-01-19 13:59:13.342 archiveDrop_cocoa[76758:10b] /Users/steve/Desktop/thinkstock/kidscolor57448860.jpg 
2012-01-19 13:59:15.014 archiveDrop_cocoa[76758:10b] /Users/steve/Desktop/thinkstock/IVDrip76801701.jpg 
2012-01-19 13:59:18.263 archiveDrop_cocoa[76758:10b] /Users/steve/Desktop/thinkstock/stk26719pin.jpg 
2012-01-19 13:59:23.414 archiveDrop_cocoa[76758:10b] /Users/steve/Desktop/thinkstock/WomanLabtop78634274.jpg 
+0

当你在上线调试器打破,你叫'componentsSeparatedByString:',什么是调试器说'thisImagePath'是? – user1118321

+0

提示:在您尝试将componentsSeparatedByString应用于它之前,NSLog thisImagePath。 –

+0

@HotLicks - 我有,我发布的这两条路径直接来自日志。 – PruitIgoe

回答

3

您的图像列表中的一个条目是一个NSArray。你需要弄清楚为什么。

3

你可能想看看NSString's -lastPathComponent-pathComponents而不是调用-componentsSeparatedByString:因为它们将可靠地解析路径。

像热舔建议,它看起来像你试图拨打电话-componentsSeparatedByStringNSArray。我会NSLog(@"imageList: %@", imageList)几个样本文件滴,看看你做了什么,或者可能里面的for循环

if (![thisImagePath isKindOfClass:[NSString class]) NSLog(@"Not a String: %@", thisImagePath); 
+0

查看上面的编辑,感谢isKindOfClass推送,清除了至少我在我的循环中得到字符串。现在要弄清楚为什么应用程序认为这些字符串中的某些字符串是下一行代码中的数组... – PruitIgoe

+0

问题解决了 - 它正在进一步发展中,我从文件中拉取元数据,特别是关键字。这些是股票照片,并没有遵守任何标准化,一些有关键字作为字符串,一些有他们作为数组。投了大家的帮助。 – PruitIgoe