2015-01-07 66 views
1

我試圖過濾一個'頻道'數組,只查找類型屬性爲'Twitter'的頻道。但是,我嘗試使用NSPredicate過濾數組返回一個空數組。這裏是我的代碼:NSPredicate returns empty array

NSMutableDictionary *officials = [decodedData valueForKey:@"officials"]; 
NSMutableArray *channels = [officials valueForKey:@"channels"]; 
NSLog(@"%@", channels); 


NSPredicate *twitterPredicate = [NSPredicate predicateWithFormat:@"type = 'Twitter'"]; 
NSLog(@"%@", [channels filteredArrayUsingPredicate:twitterPredicate]); 

這是我之前和謂詞應用後的回報:

前:

(
    (
      { 
     id = SenatorFeinstein; 
     type = Facebook; 
    }, 
      { 
     id = SenFeinstein; 
     type = Twitter; 
    }, 
      { 
     id = SenatorFeinstein; 
     type = YouTube; 
    } 
), 
    (
      { 
     id = senatorboxer; 
     type = Facebook; 
    }, 
      { 
     id = SenatorBoxer; 
     type = Twitter; 
    }, 
      { 
     id = SenatorBoxer; 
     type = YouTube; 
    } 
), 

後:

2015-01-07 10:19:31.760 voices[537:66850]() 

有沒有wr與我的謂詞過濾器?我正在使用適用於iOS的Google Civic API。任何建議表示讚賞

+0

的問題是你有兩個數組的數組的通道內。 –

+0

看起來你有一個數組包含兩個數組,每個數組包含三個字典。您的謂詞可能只是過濾頂部數組,並且找不到任何匹配,因爲它正在過濾的元素(數組)不提供該鍵。你會想改變你的邏輯來過濾子數組。 –

回答

2

試試這個,它從通道陣列內部的陣列讓所有的字典:

NSMutableDictionary *officials =[decodedData valueForKey:@"officials"]; 
NSMutableArray *channels = [officials valueForKey:@"channels"]; 
NSLog(@"%@", channels); 

NSMutableArray *onlyChannels = [[NSMutableArray alloc] init]; 
for (NSArray *array in channels) 
{ 
    for (NSDictionary *dict in array) 
    { 
     [onlyChannels addObject:dict]; 
    } 
} 


NSPredicate *twitterPredicate = [NSPredicate predicateWithFormat:@"type = 'Twitter'"]; 
NSLog(@"%@", [onlyChannels filteredArrayUsingPredicate:twitterPredicate]);