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... Commented Apr 15, 2011 at 5:59

3 Answers 3

20

Use the addObject function of you NSMutableArray.

eg.

[myNSMutableArray addObject:myString1];
[myNSMutableArray addObject:myString2];
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

You can add at NSMutableArray allocation.

Like :

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.