我試圖從文件路徑組件創建一個數組。我的文件路徑的數組(如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線步驟:
但如果我打繼續它崩潰
至於建議我改變。我的代碼來記錄數據:
//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
當你在上線調試器打破,你叫'componentsSeparatedByString:',什麼是調試器說'thisImagePath'是? – user1118321
提示:在您嘗試將componentsSeparatedByString應用於它之前,NSLog thisImagePath。 –
@HotLicks - 我有,我發佈的這兩條路徑直接來自日誌。 – PruitIgoe