2012-01-19 61 views
0

我試圖從文件路徑組件創建一個數組。我的文件路徑的數組(如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線步驟:

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

類檢查的條件永遠不會被觸發,因爲一切都是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 
+0

當你在上線調試器打破,你叫'componentsSeparatedByString:',什麼是調試器說'thisImagePath'是? – user1118321

+0

提示:在您嘗試將componentsSeparatedByString應用於它之前,NSLog thisImagePath。 –

+0

@HotLicks - 我有,我發佈的這兩條路徑直接來自日誌。 – PruitIgoe

回答

3

您的圖像列表中的一個條目是一個NSArray。你需要弄清楚爲什麼。

3

你可能想看看NSString's -lastPathComponent-pathComponents而不是調用-componentsSeparatedByString:因爲它們將可靠地解析路徑。

像熱舔建議,它看起來像你試圖撥打電話-componentsSeparatedByStringNSArray。我會NSLog(@"imageList: %@", imageList)幾個樣本文件滴,看看你做了什麼,或者可能裏面的for循環

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

查看上面的編輯,感謝isKindOfClass推送,清除了至少我在我的循環中得到字符串。現在要弄清楚爲什麼應用程序認爲這些字符串中的某些字符串是下一行代碼中的數組... – PruitIgoe

+0

問題解決了 - 它正在進一步發展中,我從文件中拉取元數據,特別是關鍵字。這些是股票照片,並沒有遵守任何標準化,一些有關鍵字作爲字符串,一些有他們作爲數組。投了大家的幫助。 – PruitIgoe