0

NSDictionaryのNSArrayがあります。すべての辞書にはキーがありますimage-path。キーの特定の値(変数
の内容)に一致する辞書のみのフィルター処理された配列を取得したいと思います。pathimage-path

この行を使用して、データの構造を検証しました(キーの画像パスの辞書のすべての値が出力されます)。

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
4

1 に答える 1

2

述語フォーマット文字列に配置したエスケープされた引用符を削除する必要があります。Apple predicate format string reference に記載されているとおり:

%@ を使用して文字列変数をフォーマット文字列に代入する場合、それらは引用符で囲まれます。動的なプロパティ名を指定する場合は、次の例に示すように、書式文字列で %K を使用します。

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

この場合の述語フォーマット文字列は、「Adam」のような firstName に評価されます。

一重引用符または二重引用符で囲まれた変数 (または置換変数文字列) により、%@、%K、または $variable がフォーマット文字列のリテラルとして解釈されるため、置換が防止されます。次の例では、述語フォーマット文字列は "%@" のような firstName に評価されます (%@ を一重引用符で囲むことに注意してください)。

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

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

于 2012-02-03T15:50:22.160 に答える