2013-01-02 12 views
6

NSPredicateの一重引用符( ')に直面しています。 NSPredicateシングルクォートの問題

NSPredicate *query = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"s_name='%@' AND s_regno=%d",Name,[RegNo intValue]]]; 

私は名前でフィルタ

は、「ジョン」は、それがOKで、全くエラー:

は、ここに私のクエリです。しかし、私は「Mary Monny」という名前を使用します。エラーがあります。

私はそれが一重引用符のためだと思います。 plsはこの問題を克服する方法を私に助けます。

おかげ

答えて

1

名= @「ベル」、あなたはこのように述語を作ってみることができた場合:

NSPredicate *query = [NSPredicate predicateWithFormat:@"name == \"%@\"", name]; 

ちょうど二重引用符で標準的な単一引用符を交換してください。

+3

あなたの入力文字列が二重引用符で囲まれている場合、何ですか? – northernman

12

はクイックフィックスとして、あなたはすべてのNSString一部を必要はありません。この置換は、predicateWithFormat:メソッドの全ポイントです!単純に次のコードを使用してください:

NSPredicate *query = [NSPredicate predicateWithFormat:@"s_name == %@ AND s_regno == %d", Name, [RegNo intValue]]; 

私は書式文字列を完全に避け、代わりにコードで述語を作成します。私はs_name like[cd] %@のように、名前の大文字小文字と発音区別符号と小文字を区別しない比較を行うためにNSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOptionを追加

NSPredicate *nameQuery = 
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_name"] 
            rightExpression:[NSExpression expressionForConstantValue:Name] 
              modifier:NSDirectPredicateModifier 
               type:NSLikePredicateOperatorType 
              options:NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption]; 

NSPredicate *regNoQuery = 
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_regno"] 
            rightExpression:[NSExpression expressionForConstantValue:RegNo] 
              modifier:NSDirectPredicateModifier 
               type:NSEqualToPredicateOperatorType 
              options:0]; 

NSPredicate *query = [NSCompoundPredicate andPredicateWithSubpredicates:@[nameQuery,regNoQuery]]; 

注意。必要がない場合は、もちろんtype:NSEqualToPredicateOperatorTypeoptions:0を使用できます。

+0

あなたの「クイックフィックス」が正しい答えです。 –

+0

これは正解とマークする必要があります。 – northernman