2011-11-11 106 views
1

我有一個名爲MyObject的對象,具有2個屬性MyObject.name,MyObject.ID 我還有一個NSMutableArray對象MyObjects來存儲MyObject對象實例。如何通過對象的屬性篩選NSArray中的對象

現在,我想過濾掉MyObject.name在MyObjects中包含char @「a」的所有實例,我該怎麼做?我嘗試使用NSDictionary & NSPredicate方法,但都失敗了。

你有什麼想法嗎?非常感謝〜

回答

0

有很多方法可以做到這一點,但這裏有一個想法。您可以循環遍歷MyObjects NSMutable數組中的所有對象,然後編寫邏輯以在每個MyObject.name中搜索@「a」,然後將不包含@「a」的那些存儲在新數組中。

// note using ARC 
NSMutableArray *myFilteredObjects = [[NSMutableArray alloc] init]; 
for(MyObject *myObject in MyObjects) 
{ 
    NSRange *range = [myObject.name rangeOfString:@"a"]; 

    if(range.location == NSNotFound) 
    { 
     // add object to new array if it doesn't contain @"a" 
     [myFilteredObjects addObject: myObject]; 
    } 
} 

// do what you want with filtered objects array