0

I've allocated a mutable array as:

self.allisonHall.hours = [[NSMutableArray alloc] initWithCapacity:7];

and filled it with data:

self.allisonHall.hours = @[
                           @[
                               @[@600,@780],
                               @[@1005,@1170]
                           ],
                           @[
                               @[@450,@585],
                               @[@705,@795],
                               @[@1005,@1140]
                           ],
                           @[
                               @[@450,@585],
                               @[@705,@795],
                               @[@1005,@1140]
                           ],
                           @[
                               @[@450,@585],
                               @[@705,@795],
                               @[@1005,@1140]
                           ],
                           @[
                               @[@450,@585],
                               @[@705,@795],
                               @[@1005,@1140]
                           ],
                           @[
                               @[@450,@585],
                               @[@705,@795],
                               @[@1005,@1140]
                           ],
                           @[
                               @[@645,@810],
                               @[@1005,@1170]
                           ],
                        ];

When I attempt to access an element using the code int open = _hours[0][i][0];

or

int open = _hours[0][i][1];

where i is 2 or 3 (depending on the number of objects in each of the 7 parent arrays, open is assigned as 146980960, clearly a memory address. When debugging, the command

po open

results in 600, 450, etc.—all correct values of the array. However, when I'm performing inequalities later in the function, they do not resolve as expected because open is 146980960 within the scope.

Why does "po" print the variable as expected while it does not exist this way in the function? And how can I resolve it?

Thanks

3
  • You haven't allocated an array and filled it with data, you've allocated an array, then replaced it with a different array, letting the original one go out of scope and get released. Commented Jul 29, 2014 at 1:02
  • If I simply change the allocation to [[NSArray alloc] init] then fill it with data will the same problem occur? Or is it necessary to initialize the array with the data? Commented Jul 29, 2014 at 1:08
  • You don't need to allocate or initialize anything. The @[…] creates an array. Just assign that directly to your NSArray * variable; don't assign something else (an empty array or otherwise) to that variable just to overwrite it a moment later. Commented Jul 29, 2014 at 1:21

1 Answer 1

2

The reason you are getting a pointer rather than the value is because you are passing a NSNumber rather than the actual value into the array. In other words, open should be a NSNumber and you can use one of its methods such as intValue to get the actual value. Hope this helped!

Sign up to request clarification or add additional context in comments.

1 Comment

That solved it. Thanks! Still getting a handle on Objective-C.

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.