0

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; 
}
2
  • int i; NSInteger *namesCount=[eCategories count]; SubCategories *subCategoriesList=[[SubCategories alloc] init];//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; } I am making some mistake that i knw......i am a begineer tryng to learn Objective c.This is the code.. Commented May 9, 2011 at 14:46
  • You should edit your answer rather than put the code in a comment where it is totally unreadable. I've done it for you this time :) Commented May 9, 2011 at 14:49

3 Answers 3

1

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"];
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

-1

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]; }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.