1

I will be putting a variety of things in this mutable array, but first I am just trying to make sure it works by putting in strings, and then pulling out the strings. Here is my code

str1=@"1";
str2=@"2";
str3=@"3";
NSMutableArray *testArray;
[testArray addObject:str1];
[testArray addObject:str2];
[testArray addObject:str3];

retrieve =[testArray objectAtIndex:1];
NSLog(@"the test number is %@",retrieve);

The problem is that my string:retrieve equals "null" after receiving the string from the array. I'm not sure what I am doing wrong, I've looked at Apple's documentation but I'm having trouble making sense of it. I know I must be interacting with the array incorrectly, but I'm not sure how exactly. Help will be appreciated. -Thank you!

1
  • testArray = [NSMutableArray new] ? Commented Nov 20, 2014 at 18:43

4 Answers 4

2

You did not initialize your testArray:

NSMutableArray *testArray = [NSMutableArray array];

You can populate the array using new syntax. If you needed mutability only to add the three items, you could use a non-mutable array instead, like this:

NSArray *testArray = @[ @"1", @"2", @"3"];

If you do need mutability, call mutableCopy:

NSMutableArray *testArray = [@[ @"1", @"2", @"3"] mutableCopy];
Sign up to request clarification or add additional context in comments.

Comments

0

Well, that's because testArray is nil. you should change the 4th line to

NSMutableArray *testArray = [NSMutableArray array];

Comments

0

You array is nil.

You are missing NSMutableArray *testArray = [NSMutableArray array];

Comments

0

To clarify what others are telling you:

This line

NSMutableArray *testArray;

Does not create an array. It creates a pointer variable that can be used to point to a mutable array. It starts out containing a zero value (nil, points to nothing.)

It's like a postal address that points to an empty lot.

You need to create (allocate) and initialize a mutable array object in order to use it. (Continuing our analogy, you have to build a house and put a mailbox in front of it before the address becomes valid.)

So you need to say:

testArray = [@[ @"1", @"2", @"3"] mutableCopy];

Breaking that down:

The inner part,

@[ @"1", @"2", @"3"]

Creates an immutable array that contains 3 string objects.

Then we ask the immutable array to create a mutable copy of itself. We save the address of that newly created mutable array into the pointer variable testArray.

We could do it in 3 steps:

NSMutableArray *testArray;
NSArray *tempArray = @[ @"1", @"2", @"3"];
testArray = [tempArray mutableCopy];

Or all at once, like @dasblinkenlight's code:

NSMutableArray *testArray = [@[ @"1", @"2", @"3"] mutableCopy];

1 Comment

Thanks Duncan! it makes sense now, some how i didn't grasp that concept by reading apples docs.

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.