2014-02-06 3 views
-1

을 사용하여 NSDictionary을 필터링하려고합니다. 오류가있는 것 같습니다.nspredicate를 사용하는 Nsdictionary 필터링

dict = [[NSDictionary alloc] initWithObjectsAndKeys:translation, @"trans", meaning, @"mean", pronounce, @"pron", theId, @"id", nil]; 

나는이 사전을 필터링 할 :

나는이 NSDictionary가 있습니다. 사전에 id 키의 값이 passedId와 동일한 경우, NSArray에 추가 : 나는 다음과 같은 코드를 사용하고

을 :

'NSUnknownKeyException', reason: '[<__NSCFConstantString 0xada8> valueForUndefinedKey:]: this class is not key value coding-compliant for the key theId. 
+0

@iMani, 코드의 첫 번째 줄은 어떨까요? – vikingosegundo

답변

0

:

NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"theId == %@", passedId]; 
NSArray *requiredRows = [[dict allKeys] filteredArrayUsingPredicate:filterPredicate]; 

날이 오류를 제공합니다 귀하의 키는 id :

theId, @"id", 

그래서 당신의 술어가 잘못된 키를 사용하고 있습니다. 다음과 같아야합니다.

[NSPredicate predicateWithFormat:@"id == %@", passedId] 

사전 및 조건 자의 키가 일치해야하기 때문입니다.


나는 원래 [dict allKeys]을 사용하는 것을 알지 못했습니다. 이것은 하나의 사전에서 모든 키 배열을 가져옵니다. 여기에는 가치가 없으며 그것을 필터링하는 포인트가 없습니다.

사전 배열이 있어야하며 해당 배열에서 조건자를 실행해야합니다. 그러면 결과에는 id과 일치하는 사전 만 포함됩니다.

+0

이 클래스는 키 ID에 대해 키 값 코딩을 준수하지 않습니다. ' – user3258468

0

코드가 전혀 이해가되지 않습니다. 더 라인으로 분할하는 것은 명백한 만들려면 :

NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"theId == %@", passedId]; 
NSArray *allKeys = [dict allKeys]; 
NSArray *requiredRows = [allKeys filteredArrayUsingPredicate:filterPredicate]; 

해서 AllKeys는 NSStrings의 배열이며이 @"id", @"mean", @"pron", @"trans" 것 같습니다. 필터링은 기본적으로 각 NSString에 대해 [NSString theId]을 호출하기 때문에 @"theId"을 필터링 할 수 없으며이 메서드의 결과는 조건 자에 지정한 문자열과 비교됩니다. 그게 예외가 어디에서 왔는지 NSString은 theId이라는 메서드를 가지고 있지 않습니다.

그리고 술어로 self == %@을 사용했기 때문에 작동한다고하더라도, 다시 얻을 수있는 결과는 @"theId"입니다.

나는 당신이 정말로 원하는 것이 확실치 않지만, 이렇게 작동하지 않을 것이다.

관련 문제