1

my attempt to init an array with a number of bool values using:

[myArray initWithObjects:[NSNumber numberWithBool:YES], 
                         [NSNumber numberWithBool:YES], 
                         [NSNumber numberWithBool:YES],
                         nil];

seems to fail since the debugger shows an empty array after this statement is carried out ... Any clues?

1 Answer 1

3

Make sure you are alloc-ing the object, as well, i.e.:

NSArray *myArray = [[NSArray alloc] initWithObjects:...];
...
[myArray release];

Or:

NSArray *myArray = [[[NSArray alloc] initWithObjects:...] autorelease];

Or:

NSArray *myArray = [NSArray arrayWithObjects:...];
Sign up to request clarification or add additional context in comments.

3 Comments

or better use NSArray arrayWithObjects: to do both at once
Sometimes it is preferable to alloc-init, to be able to release the array as soon as it is no longer needed. But either way works.
Thanks to all of you. I found the bug. I did an initWithContentsOfFile that, when unsuccessful, should load the defaults. The missing link was that I had to alloc/init in case the loading from file fails.

Your Answer

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