2013-03-12 20:50:05.572 appname[4102:702] the array: ( { groupID = 1; name = "Dan"; },{ groupID = 1; name = "Matt"; },{ groupID = 2; name = "Steve"; },{ groupID = 2; name = "Mike"; },{ groupID = 3; name = "John"; },{ groupID = 4; name = "Kevin"; })
这就是我想要的样子:
2013-03-12 20:50:05.572 appname[4102:702] the array: ( { groupID = 1; name1 = "Dan"; name2 = "Matt"; },{ groupID = 2; name1 = "Steve"; name2 = "Mike"; },{ groupID = 4; name = "Kevin"; })
编辑:
我试过&失败了许多尝试,大多数沿着这样的事情(马虎的娱乐,但给出一个想法):
int IDNum = 0;for (NSDictionary *arrObj in tempArr){ Nsstring *check1 = [Nsstring stringWithFormat:@"%@",[arrObj valueForKey:@"groupID"]]; Nsstring *check2 = [Nsstring stringWithFormat:@"%@",[[newDict valueForKey:@"groupID"]]; if (check1 == check2){ Nsstring *nameStr = [Nsstring stringWithFormat:@"name_%d",IDNum]; [newDict setValue:[arrObj valueForKey:@"name"] forKey:nameStr]; } else { [newDict setValue:arrObj forKey:@"object"]; } IDNum++;}解决方法
NSArray *array = @[@{@"groupID" : @"1",@"name" : @"matt"},@{@"groupID" : @"2",@"name" : @"john"},@{@"groupID" : @"3",@"name" : @"steve"},@{@"groupID" : @"4",@"name" : @"alice"},@{@"groupID" : @"1",@"name" : @"bill"},@"name" : @"bob"},@"name" : @"jack"},@"name" : @"dan"},@"name" : @"kevin"},@"name" : @"mike"},@"name" : @"danIEl"},];NSMutableArray *resultArray = [NSMutableArray new];NSArray *groups = [array valueForKeyPath:@"@distinctUnionOfObjects.groupID"];for (Nsstring *groupID in groups){ NSMutableDictionary *entry = [NSMutableDictionary new]; [entry setobject:groupID forKey:@"groupID"]; NSArray *groupnames = [array filteredArrayUsingPredicate:[nspredicate predicateWithFormat:@"groupID = %@",groupID]]; for (int i = 0; i < groupnames.count; i++) { Nsstring *name = [[groupnames objectAtIndex:i] objectForKey:@"name"]; [entry setobject:name forKey:[Nsstring stringWithFormat:@"name%d",i + 1]]; } [resultArray addobject:entry];}NSLog(@"%@",resultArray);
输出:
( { groupID = 3; name1 = steve; name2 = jack; name3 = danIEl; },{ groupID = 4; name1 = alice; name2 = dan; },{ groupID = 1; name1 = matt; name2 = bill; name3 = kevin; },{ groupID = 2; name1 = john; name2 = bob; name3 = mike; } )总结
以上是内存溢出为你收集整理的ios – 通过对具有匹配的身份号码的对象进行分组来重建NSArray?全部内容,希望文章能够帮你解决ios – 通过对具有匹配的身份号码的对象进行分组来重建NSArray?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)