ファイルパスコンポーネントから配列を作成しようとしています。私は(NSStringsなど)のファイルパスの配列を持っていると私はそのようにように、それぞれを破壊、それらを介してループしています:Objective-C:NSStringからNSStringを作成する
//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
イメージリストは、ファイルの配列であるビューにドロップ。この問題が発生し始めたので、私は一度に1つのファイルを削除しています。たとえば:
このファイルは動作しませんでした:
/Users/steve/Desktop/thinkstock/PT121211_PI_bariatric.tif
これは、私が正しくエラーメッセージを理解していれば、私はNSArrayのにcomponentsSeparatedByStringセレクタを適用しようとしていますので、
/Users/steve/Desktop/thinkstock/Studentexhausted82557038.jpg
をしましたそのセレクタをサポートしていません。しかし、私のループでは、オブジェクトが配列だった場合はNSStringを呼び出す必要があります。 (そして、私は99%の画像リストのインデックス0にあるオブジェクトが文字列であることを確信しています)
私の目的は、ファイルパスからファイル名を取得することです。 ?
私はcomponentsSeparatedByStringラインでデバッグポイントを置く(ステップスルーすると、私が計画どおりに動作するようです:
しかし、私はそれがクラッシュし続けるヒットした場合
としては、私が変更を提案しました。すべてがNSSであるとして、
//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];
トリガーされることはありません飽きないクラスのチェックのための条件:私のコードは、データをログに記録しますクラスをする。ただし、一部のファイルは機能しますが、一部のファイルは機能しません。
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
内部を見る:'、デバッガを何ん'thisImagePath'とは? – user1118321
ヒント:componentsSeparatedByStringを適用する前にNSLog thisImagePathを呼び出してください。 –
@HotLicks - 私が投稿した2つのパスは、ログから直接取得したものです。 – PruitIgoe