2012-01-19 6 views
0

ファイルパスコンポーネントから配列を作成しようとしています。私は(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ラインでデバッグポイントを置く(ステップスルーすると、私が計画どおりに動作するようです:

enter image description here

しかし、私はそれがクラッシュし続けるヒットした場合

としては、私が変更を提案しました。すべてが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 
+0

内部を見る:'、デバッガを何ん'thisImagePath'とは? – user1118321

+0

ヒント:componentsSeparatedByStringを適用する前にNSLog thisImagePathを呼び出してください。 –

+0

@HotLicks - 私が投稿した2つのパスは、ログから直接取得したものです。 – PruitIgoe

答えて

3

イメージリスト内のエントリの1つはNSArrayです。その理由を理解する必要があります。

3

あなたは、彼らは確実にパスを解析するので、代わりに-componentsSeparatedByString:を呼び出すのではNSString's -lastPathComponentまたは-pathComponentsを見てみたいことがあります。

ホットリックスのように、-componentsSeparatedByStringNSArrayに電話しているようです。私はいくつかのサンプルファイルの滴のためNSLog(@"imageList: %@", imageList)が希望し、あなたが `componentsSeparatedByString呼び出す行にデバッガで中断すると、あなたが取得しているもので、またはおそらくループ

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

上記の編集を参照してください、isKindOfClassプッシュのおかげで、少なくとも私はループ内の文字列を取得しています。今度は、なぜアプリケーションがその文字列の一部が次のコード行の配列であると思うのかを理解するために... – PruitIgoe

+0

問題が解決しました。ファイルからメタデータ、特にキーワードを引き出しています。これらはストック写真であり、標準化を遵守していないものもあれば、文字列としてキーワードを持つものもあれば、配列として持つものもあります。みんなに投票して助けを求めた。 – PruitIgoe

関連する問題