ios - Group and count NSArray Objects -
i have nsarray of objects. each object has following properties/attributes name,id,type
. name
, id
dependent . i.e. if id 12345 , id
aaaa object id
12345 name
aaaa.
what have particular type
bedtype. want consider objects of bedtype , group them id
, count how many objects belong each group. want output name
, count each group.
you fx create dictionary keys names , values counts of objects. can iterate on array ,
nsmutabledictionary *countsbyname = [@{} mutablecopy]; for(object *object in myobjectarray){ nsnumber *count = countsbyname[object.name] if(count) countsbyname[object.name] = @([count intvalue] + 1); else countsbyname[object.name] = @1; }
you need make sure consider object type bedtype.
Comments
Post a Comment