I’m trying to make an array from file path components. I have an array of file paths (as NSStrings) and I am looping through them, then breaking each down like so:
//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:@"/"];
Half the time my program is crashing here. I get the following error message:
-[NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x1a7940
imageList is an array of files dropped onto a view. Because this problem started popping up I am only dropping one file at a time. For example:
This file did not work:
/Users/steve/Desktop/thinkstock/PT121211_PI_bariatric.tif
This did
/Users/steve/Desktop/thinkstock/Studentexhausted82557038.jpg
so, if I understand the error message correctly I am trying to apply the componentsSeparatedByString selector to an NSArray, which doesn’t support that selector. But in my loop I am calling for NSString, if the object was an array shouldn’t I crash there? (And I am 99% sure the object at index 0 of imageList is a string.)
My objective is to get the file name from the file path, is there a better way of doing it than the approach I am taking?
When I step through (putting a debug point at the componentsSeparatedByString line it seems to work as I planned:

But if I hit continue it crashes.
As suggested I changed my code to log data:
//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];
The conditional for the class check never get triggered, as everything is of NSString class. However, some files work, some don’t…
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
When you’re 100% sure that you really get a NSString here, why don’t you use
[thisImagePath lastPathComponent]?http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/lastPathComponent