2017-04-10 8 views
0

私はProductDataという配列を持っていますが、それらをcategoryに基づいてフィルタリングし、次のように適用しました。Predicateが返されました。キャッチされない例外により「NSUnknownKeyException」、 理由にアプリを終了NSPRedicate値ForUndefinedKey

pTempElements =[[NSMutableArray alloc] initWithArray: [self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"category = %@", @"4"]]]; 

:「[valueForUndefinedKeyは:]:このクラス は、キー値コーディング準拠のキーカテゴリのではありません。」

ここではpElements

po self.pElements 
<__NSArrayM 0x174241e30>(
<ProductData: 0x17427ce00>, 
<ProductData: 0x17427cd80>, 
<ProductData: 0x17427ce40>, 
<ProductData: 0x17427ce80>, 
) 

の見落としであるProductData.m

#import "ProductData.h" 

@implementation ProductData 
@synthesize pId, pImage, pPrice, pName, pCategory 

-(id)initWithDictionary:(NSDictionary *)aDict{ 
    self = [self init]; 
    if (self){ 
     self.pId = [aDict objectForKey:@"id"]; 
     self.pPrice = [aDict objectForKey:@"price"]; 
     self.pImage = [aDict objectForKey:@"imagePath"]; 
     self.pCategory = [aDict objectForKey:@"category"]; 
     self.pName = [aDict objectForKey:@"name"]; 
    } 
    return self; 
} 
+2

以下のようにコードを書き直す必要がありますか? pTempElements = [[NSMutableArray alloc] initWithArray:[self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@ "pCategory ==%@"、@ "4"]]]; –

+0

はい、うまくいきました。 – hotspring

答えて

1

あなたは、配列をフィルタリングすることを計画していることによって、オブジェクトのプロパティ名を与える必要があります。ここで私はそれがpCategoryだと思います。あなたはこれを試すことができます

pTempElements =[[NSMutableArray alloc] initWithArray: [self.pElements filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pCategory == %@", @"4"]]];