0

我有如下 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...");
}

但我得到not found...mCountryId 的输出为 5。

4

2 回答 2

3

使用 anNSDictionary代替,并通过 Id 键入您的条目;例如:

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];
}
于 2013-10-01T09:48:41.527 回答
2
NSPredicate * filter = [NSPredicate predicateWithFormat:@"Id = 5"];
NSArray * filtered = [array filteredArrayUsingPredicate:filter];

第一个对象filtered是字典Id = 5

于 2013-10-01T09:48:28.783 回答