2

私は非常に単純な NSPredicate を持っています:

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"name beginswith '%@'", theString];
[matchingTags filterUsingPredicate:sPredicate];

これにより、theString == "p" の場合、配列の結果は 0 になります。

ただし、これを行うと:

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"name beginswith 'p'"];
[matchingTags filterUsingPredicate:sPredicate];

予想どおり、100 を超える結果が得られます。

NSLog() で "theString" を確認しましたが、その値は正しいです。重大な秘密を見逃しているような気がします。おそらく、文字ではなく文字列を使用しているためでしょうか?

考え?

4

1 に答える 1

4

こちらのドキュメントをご覧ください

%@ (%@ のような firstName など) を使用した変数置換を使用すると、引用符が自動的に追加されます。

つまり、基本的には、p ではなく「p」で始まる名前を探しています。コードを次のように変更します。

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"name beginswith %@", theString];

動作するはずです

于 2010-06-28T17:03:20.633 に答える