【发布时间】:2012-01-19 18:16:28
【问题描述】:
我正在尝试从文件路径组件创建一个数组。我有一个文件路径数组(作为 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
【问题讨论】:
-
当您在调用
componentsSeparatedByString:的行上中断调试器时,调试器说thisImagePath是什么? -
提示:在尝试将 componentsSeparatedByString 应用到它之前,NSLog thisImagePath。
-
@HotLicks - 我有,我发布的那两条路径直接来自日志。
-
@user118321 - NSCFString* thisImagePath 0x8b1840
标签: objective-c cocoa nsstring nsarray