Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add object multi NSArray in one NSMutableArray

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 
}
like image 988
janatan Avatar asked Apr 15 '13 10:04

janatan


People also ask

What is difference between NSArray and NSMutableArray?

The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.

What's a difference between NSArray and NSSet?

The main difference is that NSArray is for an ordered collection and NSSet is for an unordered collection. There are several articles out there that talk about the difference in speed between the two, like this one. If you're iterating through an unordered collection, NSSet is great.

How do you declare NSArray in Objective C?

In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method. id objects[] = { someObject, @"Hello, World!", @42 }; NSUInteger count = sizeof(objects) / sizeof(id); NSArray *array = [NSArray arrayWithObjects:objects count:count];

Is NSMutableArray thread safe?

In general, the collection classes (for example, NSMutableArray , NSMutableDictionary ) are not thread-safe when mutations are concerned. That is, if one or more threads are changing the same array, problems can occur.


2 Answers

You can use addObjectsFromArray: from NSMutableArray class

all = [[NSMutableArray alloc]init];
[all addObjectsFromArray:animal];
[all addObjectsFromArray:color];
like image 97
Aravindhan Avatar answered Sep 22 '22 20:09

Aravindhan


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];
like image 45
Leo Chapiro Avatar answered Sep 20 '22 20:09

Leo Chapiro