开发者

object array to NSMutable array [duplicate]

This question already has an answer here: Getting an array of a property from an object array (1 answer) Closed 7 years ago.

I have an array of Objects.Object contains three values, id,name,value.I want to parse name from this object array and want to create a different array of names only....Can anyone help Plz.开发者_如何学编程....

int i; 
NSInteger *namesCount=[eCategories count]; //eCategories is an object array 
SubCategories *subCategoriesList=[[SubCategories alloc] init];
//SubCategories is a NSObject class containing cat_name,cat_id,cat_value. 
NSMutableArray *nameArray=[[NSMutableArray alloc]init]; 
for(i=0;i<namesCount;i++)
{ 
    subCategoriesList=[eCategories objectAtIndex:i]; 
    nameArray=subCategoriesList.cat_name; 
}


NSArray has a method -valueForKey: which does exactly what you want in one message

Returns an array containing the results of invoking valueForKey: using key on each of the array's objects.

NSArray* nameArray = [eCategories valueForKey: @"cat_name"];


How about

[objects valueForKeyPath: @"@distinctUnionOfArrays.name"]

Unless, of course, the objects inside the array are not NSDictionaries with "name" as the key for the names. You're not telling us enough.


int i;

NSInteger *namesCount=[eCategories count]; //eCategories is an object array

SubCategories *subCategoriesList=[[SubCategories alloc] init];

//SubCategories is a NSObject class containing cat_name,cat_id,cat_value.

NSMutableArray *nameArray=[[NSMutableArray alloc]init];

for(i=0;i

[nameArray addobject:subCategoriesList.cat_name]; }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜