0

それぞれ可変長の文字列の配列 (5k 要素以上) があります。NSPredicate を使用して、配列内の特定の文字列を見つけることができます (それを理解するのに少し時間がかかりました)。次に、長さが N より大きい要素を見つける必要があります。

ドキュメントを調べたところ、Length は Predicate Programming Guide で使用できる関数の 1 つではないようです。

前もって感謝します。

イマグマスト

4

2 に答える 2

2
NSArray * words= [allLinedStrings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > %d",N]];

N は、質問で述べたように整数です。

于 2012-09-09T18:44:48.317 に答える
-1

私は NSPredicate に詳しくないので、それを使用した解決策を提供することはできませんが、単純な NSString メソッドを使用することはできませんでした:

//This is the length of the string you want to check
int threshold = 5;

//Iterates through all elements in the array
for(int index = 0; index < [stringArray count]; index++) {

    //Checks if the length of the string stored at the current index is greater than N
    if([[stringArray objectAtIndex:index] length] > threshold) {

        //String length is greater than N
    }
}

必要に応じて、次の行を置き換えることで、配列に文字列のみが含まれている (安全のためにパフォーマンスが犠牲になる) かどうかを確認するチェックを追加できます。

if([[stringArray objectAtIndex:index] length] > threshold) 

これとともに:

if([[stringArray objectAtIndex:index] length] > threshold && [[stringArray objectAtIndex:index] isKindOfClass[NSString class]]) 

これが行うことは、現在のインデックスのオブジェクトが NSString であることを確認することです。そうでない場合は、ランタイム エラーが発生します。

于 2012-09-09T18:44:46.500 に答える