object array to NSMutable array [duplicate]
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]; }
精彩评论