我有NSMutableArray数据如下。
(
{
Id = "-1";
NameEn = Country;
},
{
Id = 14;
NameEn = Iran;
},
{
Id = 11;
NameEn = Jordan;
},
{
Id = 5;
NameEn = "United Arab Emirates";
},
{
Id = 4;
NameEn = "Suadi Arabia";
},
{
Id = 3;
NameEn = Kuwait;
},
{
Id = 10;
NameEn = Yemen;
},
{
Id = 6;
NameEn = Oman;
},
{
Id = 12;
NameEn = Syria;
},
{
Id = 7;
NameEn = Qatar;
},
{
Id = 13;
NameEn = Lebanon;
},
{
Id = 1;
NameEn = Egypt;
},
{
Id = 8;
NameEn = "Bahrain Kingdom";
}
)
我想找到Id = 5的位置。
知道我该怎么办?
我在下面尝试过。
NSString *myCountry = [[NSUserDefaults standardUserDefaults] valueForKey:@"mCountryId"];
NSUInteger indexOfTheObject = [feedsCountry indexOfObject: myCountry];
NSLog(@"indexOfTheObject===%i==%@", indexOfTheObject, myCountry);
if (NSNotFound == indexOfTheObject) {
NSLog(@"not found...");
}
但是mCountryId的输出为not found...
为5。
答案 0 :(得分:4)
改为使用NSDictionary
,然后按ID键入您的输入; e.g:
NSMutableDictionary* dictionary = [NSMutableDictionary new];
[dictionary setObject:@"Egypt" forKey:@"1"];
// (etc...)
编辑:如果您已有数组而无法更改,请使用for
这样的循环:
for(NSDictionary* entry in givenArray){
NSString* key = [entry objectForKey:@"Id"];
NSString* value = [entry objectForKey:@"NameEn"];
[dictionary setObject:value forKey:key];
}
答案 1 :(得分:2)
NSPredicate * filter = [NSPredicate predicateWithFormat:@"Id = 5"];
NSArray * filtered = [array filteredArrayUsingPredicate:filter];
filtered
中的第一个对象是带有Id = 5