2016-02-12 49 views
1

我有一個NSMutableArray與超過150+的對象,我需要創建一個新的數組基於這個關鍵字「名稱」與輸入名稱匹配。基於密鑰與謂詞不工作排序NSMutableArray?

所以我得到這個謂詞代碼計算器

NSMutableArray *listForm=[[NSMutableArray alloc]init]; //copy Original Array 
    listForm=[arr valueForKey:@"data"]; //copied 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@", [selectedDrug valueForKey:@"name"]]; //checking name 

    NSLog(@"%@",[selectedDrug valueForKey:@"name"]); //my input name for matching 

    [listForm filteredArrayUsingPredicate:predicate]; 


    NSLog(@"%@",listForm);//final result 

現在我得到正確的結果,但與我得到額外的對象

例如像 考慮,這是我的對象

的大名單一起
[{ 
    name:"john", 
    location:"washington", 
    age:23 
},{ 
    name:"Zirad kan", 
    location:"iceland", 
    age:23 
},{ 
    name:"john", 
    location:"usa", 
    age:43 
},{ 
    name:"riya", 
    location:"india", 
    age:20 
},{ 
    name:"Rachel", 
    location:"netherland", 
    age:33 
},{ 
    name:"john Mark", 
    location:"washington", 
    age:23 
},{ 
    name:"john Doe", 
    location:"washington", 
    age:23 
}] 

從這我需要所有與「約翰」完全匹配的名稱

所以結果會是這樣

[{ 
     name:"john", 
     location:"washington", 
     age:23 
    },{ 
     name:"john", 
     location:"usa", 
     age:43 
    }] 

但是,當我在上面的代碼IAM的使用越來越以此作爲最終結果

[{ 
     name:"john", 
     location:"washington", 
     age:23 
    },{ 
     name:"john", 
     location:"usa", 
     age:43 
    },{ 
     name:"john Mark", 
     location:"washington", 
     age:23 
    },{ 
     name:"john Doe", 
     location:"washington", 
     age:23 
    }] 

如何刪除「李四」和「約翰·馬」像我只需要一個匹配「john」的特定文本。

請幫助我

+0

看到這個鏈接可能會幫助你http://stackoverflow.com/questions/11597508/nspredicate-exact-match-with-string –

+0

'predicateWithBlock:'? –

回答

2

[NSArray filteredArrayUsingPredicate:]返回篩選數組,因此:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@", 
    selectedDrug[@"name"]]; 
NSArray *filtered = [arr[@"data"] filteredArrayUsingPredicate:predicate]; 

注:這是假定兩者arrselectedDrugNSDictionary實例。

+0

讓我現在試試這個 – Bangalore

+0

其工作..coollll – Bangalore