2012-02-03 9 views
0

私はNSDictionaryのNSArrayを持っています。すべての辞書にはimage-pathというキーがあります。
キーimage-pathの特定の値(変数pathの内容)に一致する辞書のみのフィルタリングされた配列を取得したいとします。NSPredicateが 'Unknown number typeまたはnilを算術関数式に渡しました。' QuickLookプラグインで

私はこのラインを使用しているデータの構造(それがキー画像パスのために私の辞書のすべての値を出力します)検証:

:私はこのコードを使用しています

NSLog(@"array key paths: %@", [mountedImages valueForKeyPath:@"image-path"]); 

NSString *path = @"value-I-want-to-match"; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(image-path LIKE[cd] \"%@\")", path]; 
NSArray *filteredArray = nil; 
filteredArray = [mountedImages filteredArrayUsingPredicate:predicate]; 

は、最後の行はクラッシュし、次のエラー生成:

[ERROR] Computing 6571367.19831142 raised 'Unknown number type or nil passed to arithmetic function expression.' 

私はこれをやっているのQuickLookプラグイン。 gdbでコードをステップ実行することはできますが、トレースを取得することはできません。

[...] 
[ERROR] Computing 6571367.19831142 raised 'Unknown number type or nil passed to arithmetic function expression.' 
[Switching to process 23335 thread 0x8903] 
[Switching to process 23335 thread 0xa703] 
[Switching to process 23335 thread 0x8903] 
Program ended with exit code: 0 

答えて

2

述語フォーマット文字列に置いたエスケープ引用符を削除する必要があります。アップル述語形式の文字列参照に記載されているように:あなたのケースでは

When string variables are substituted into a format string using %@ , they are surrounded by quotation marks. If you want to specify a dynamic property name, use %K in the format string, as shown in the following example.

NSString *attributeName = @"firstName"; 
NSString *attributeValue = @"Adam"; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@", attributeName, attributeValue]; 

The predicate format string in this case evaluates to firstName like "Adam".

Single or double quoting variables (or substitution variable strings) cause %@, %K, or $variable to be interpreted as a literal in the format string and so prevent any substitution. In the following example, the predicate format string evaluates to firstName like "%@" (note the single quotes around %@).

NSString *attributeName = @"firstName"; 
NSString *attributeValue = @"Adam"; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like '%@'", attributeName, attributeValue]; 

、あなたの述語は、あなたの配列をフィルタリングするために使用する場合、正しく評価するために失敗し、image-path LIKE[cd] "%@"として作成されています。

関連する問題