Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want add object from 2 NSArray to NSMutableArray. I dont know about this.

this my code:

@interface ViewController : UITableViewController
{
    NSArray *animal;
    NSArray *color;
    NSMutableArray *all;
}


@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    animal = [[NSArray alloc]initWithObjects:@"Lion",@"Tiger",@"Dog",@"Cat",@"Sheep",@"Wolf", nil];
    color = [[NSArray alloc]initWithObjects:@"Blue",@"Red",@"Yellow",@"Green",@"Black", nil];

    all = ??? ; //how to add object from animal and color array in all 
}
share|improve this question

2 Answers

You can use addObjectsFromArray: from NSMutableArray class

all = [[NSMutableArray alloc]init];
[all addObjectsFromArray:animal];
[all addObjectsFromArray:color];
share|improve this answer
2  
and don't forget to initialize all ... – Martin R Apr 15 at 10:58
there is an error in the initialization, you miss a '[', and you can use initWithArray avoiding one instruction – tkanzakic Apr 15 at 11:01
1  
@duDE : In fact I was sent to human verification also while answering this question. :) – user231089 Apr 15 at 11:04
1  
lol - Me too :) – duDE Apr 15 at 11:09

Try this:

animal = [[NSArray alloc]initWithObjects:@"Lion",@"Tiger",@"Dog",@"Cat",@"Sheep",@"Wolf", nil];
color = [[NSArray alloc]initWithObjects:@"Blue",@"Red",@"Yellow",@"Green",@"Black", nil];

all = [[NSMutableArray alloc] init];
[all addObjectsFromArray:animal];
[all addObjectsFromArray:color];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.