gpt4 book ai didi

Objective-C:从 NSString 生成 NSArray

转载 作者:行者123 更新时间:2023-12-03 17:28:14 26 4
gpt4 key购买 nike

我正在尝试从文件路径组件创建一个数组。我有一个文件路径数组(如 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 是拖放到 View 上的文件数组。因为这个问题开始出现,所以我一次只删除一个文件。例如:

此文件不起作用:

/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];

类检查的条件永远不会被触发,因为一切都是 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

最佳答案

当你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

关于Objective-C:从 NSString 生成 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8931185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com