我正在尝试从文件路径组件创建一个数组。我有一个文件路径数组(作为NSStrings),我循环遍历它们,然后像这样分解每个:
//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行放置一个调试点,它似乎按照我的计划工作:
但如果我点击继续它会崩溃。
根据建议我将代码更改为记录数据:
//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];
类检查的条件永远不会被触发,因为所有内容都是NSString类。但是,有些文件可以工作,有些文件没有...
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 :(得分:4)
当你100%确定你真的在这里获得NSString时,为什么不使用[thisImagePath lastPathComponent]
?
答案 1 :(得分:3)
图像列表中的一个条目是NSArray。你需要找出原因。
答案 2 :(得分:3)
您可能需要查看NSString's -lastPathComponent
或-pathComponents
,而不是调用-componentsSeparatedByString:
,因为它们会可靠地解析路径。
就像Hot Licks建议的那样,您似乎试图在-componentsSeparatedByString
上致电NSArray
。我会NSLog( @"imageList: %@", imageList )
查看一些示例文件,看看你得到了什么,或者可能在for循环中看看
if ( ![thisImagePath isKindOfClass:[NSString class] ) NSLog( @"Not a String: %@", thisImagePath );