1

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.

1
  • 2
    use addObject: from nsmutable array i hope this is enough for you...
    – ajay
    Apr 15, 2011 at 5:59

3 Answers 3

20

Use the addObject function of you NSMutableArray.

eg.

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

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);
0

You can add at NSMutableArray allocation.

Like :

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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