【问题标题】:Objective-C: Making an NSArray from an NSStringObjective-C:从 NSString 制作 NSArray
【发布时间】: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


【解决方案1】:

当你 100% 确定你真的在这里得到一个 NSString 时,为什么不使用[thisImagePath lastPathComponent]

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/lastPathComponent

【讨论】:

  • 你赢得了让我使用 lastPathComponent 的胜利...感谢这有很大帮助。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    您可能需要查看NSString's -lastPathComponent-pathComponents 而不是调用-componentsSeparatedByString:,因为它们会可靠地解析路径。

    就像 Hot Licks 建议的那样,您似乎正试图在 NSArray 上致电 -componentsSeparatedByString。我会 NSLog( @"imageList: %@", imageList ) 获取一些示例文件删除,看看你得到了什么,或者可能在 for 循环中

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

    【讨论】:

    • 请参阅上面的编辑,感谢 isKindOfClass 推送,至少我在循环中得到了字符串。现在要弄清楚为什么应用程序认为其中一些字符串是下一行代码中的数组...
    • 问题解决了 - 它发生在更远的地方,我从文件中提取元数据,特别是关键字。这些是库存照片,不遵守任何标准化,有些将关键字作为字符串,有些将它们作为数组。投票给每个人以寻求帮助。
    猜你喜欢
    • 1970-01-01
    • 2010-12-22
    • 2018-04-27
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多