2011-05-31 45 views
4

的陣列工作,我有一個NSPredication與格式是這樣的:@"Status CONTAINS[cd] shipped"NSPredicate不使用詞典

我有字典的數組,這裏有一個例子:

{ 
     { 
     Category = "Category 4"; 
     Comp = "Category 4"; 
     Depth = 02; 
     Grade = New; 
     Length = 02; 
     Location = "Thousand Oaks, CA"; 
     Name = "3ply Mat"; 
     Owner = "Owner 3"; 
     PUP = 2; 
     Status = Shipped; 
     "Unit Number" = 20110507113852351; 
     Width = 02; 
    }, 
     { 
     Category = "Category 4"; 
     Comp = "Category 4"; 
     Depth = 02; 
     Grade = New; 
     Length = 02; 
     Location = "Thousand Oaks, CA"; 
     Name = "3ply Mat"; 
     Owner = "Owner 3"; 
     PUP = 2; 
     Status = Shipped; 
     "Unit Number" = 20110516094641587; 
     Width = 02; 
    } 
) 

但它返回一個空數組。我究竟做錯了什麼?謝謝

回答

3

您可以使用關鍵字MATCHES,CONTAINS等,對字符串。當狀態不是字符串,我想,你需要檢查它像這樣,

[NSPredicate predicateWithFormat:@"Status = %d", Shipped]; 
7

你想:

[NSPredicate predicateWithFormat:@"Status =[cd] 'shipped'"]; 

或者:

[NSPredicate predicateWithFormat:@"Status =[cd] %@", @"shipped"]; 

你的問題是,你的謂詞格式有shipped,當它應該有'shipped'。沒有單引號意味着它試圖比較[dictionary valueForKey:@"Status"][dictionary valueForKey:@"shipped"],而不是字符串文字@"shipped"