如何在NSMutablearray中获取特定对象的索引?

时间:2013-07-09 08:36:50

标签: ios object indexing nsmutablearray

我有NSMutablearray这样的(它是一个JSON)

{
   "notification_list":[
      {
         "TYPE":"ARTIST",
         "ID":"07",
         "DTEXT":"Now you can listen to the songs of ARTIST1",
         "ADDEDDATE":"27th June, 2013 4:44 pm."
      },
      {
         "TYPE":"BRAND",
         "ID":"http:\/\/www.me.lk\/",
         "DTEXT":"M Entertainment have been joined with Mobile Music",
         "ADDEDDATE":"27th June, 2013 3:37 pm."
      },
      {
         "TYPE":"ARTIST",
         "ID":"03",
         "DTEXT":"Now you can listen to the songs of ARTIST2",
         "ADDEDDATE":"27th June, 2013 3:37 pm."
      },
      {
         "TYPE":"ALBUM",
         "ID":"Nava Ridgma",
         "DTEXT":"Get new album of ARTIST3, Nava ridma",
         "ADDEDDATE":"27th June, 2013 3:37 pm."
      },
      {
         "TYPE":"SONG",
         "ID":"05",
         "DTEXT":"New Song added to the Mobile Music - Hanthane Kandumuduna by ARTIST5, Album - Aradhana",
         "ADDEDDATE":"27th June, 2013 3:37 pm."
      }
   ]
}

现在我想在此NSMutablearray

中获取此特定对象的索引
{
   "TYPE":"ALBUM",
   "ID":"Nava Ridgma",
   "DTEXT":"Get new album of ARTIST3, Nava ridma",
   "ADDEDDATE":"27th June, 2013 3:37 pm."
}

如何从此NSMutablearray搜索此对象并获取索引。

任何人都可以帮助我。

由于

4 个答案:

答案 0 :(得分:7)

尝试

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"TYPE == [cd] %@",@"ALBUM"];

NSInteger index = [array indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    return [predicate evaluateWithObject:obj];
}];

NSLog(@"Index of object %d",index);

答案 1 :(得分:2)

NSMutableArray *myArray

考虑myArray包含您提供的数据。

NSPredicate *thePredicate = [NSPredicate predicateWithFormat:@"%K LIKE %@", @"TYPE", @"ALBUM"];
NSArray *filteredList = [myArray filteredArrayUsingPredicate:thePredicate];

NSDictionary *selectedDict = [[NSDictionary alloc]init];
selectedDict = [filteredList lastObject];

int index;
index = [arr indexOfObject:selectedDict];

这将为您提供type = album。

的字典索引

希望这会对你有所帮助。

答案 2 :(得分:0)

在您的情况下,您的consol描述为字典,您可以通过indexOfObject的{​​{1}}

获取对象的特定索引
NSMutableArray

答案 3 :(得分:0)

首先必须解析JSON并保存到NSMutableArray中。  Parse JSOn tutorial: 最后,你在NSMutableArray中搜索你想要的类型。