Rani
Rani

Reputation: 3453

Add NSStrings to mutable array

I created a mutable array and I have two NSString variables. Now I want to add these two NSStrings to my array. How is this possible? Thanks.

Upvotes: 1

Views: 20409

Answers (3)

Chetan Bhalara
Chetan Bhalara

Reputation: 10344

You can add at NSMutableArray allocation.

Like :

NSMutableArray *test = [NSMutableArray arrayWithObjects:@"test1",@"test2",nil];

Upvotes: 0

Konrad77
Konrad77

Reputation: 2535

Jhaliya's answer is correct. +1 vote.

I added a immutable version so you can see the difference. If you dont want to remove or add more objects (NSStrings) to your container, I would recommend using an Immutable version.

Mutable version:

NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
NSString *string_one = @"One"];
[mutableArray addObject:string_one];
//Or
[mutableArray addObject:@"Two"];
NSLog(@"%@", mutableArray);

Immutable version

NSArray *immutableArray = [NSArray arrayWithObjects:@"One", @"Two", nil];
NSLog(@"%@", immutableArray);

Upvotes: 0

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31730

Use the addObject function of you NSMutableArray.

eg.

[myNSMutableArray addObject:myString1];
[myNSMutableArray addObject:myString2];

Upvotes: 20

Related Questions