1

Just a simple question, I hope.

When creating an array using alloc and init, i.e. without specifying 'initWith...' or preloading it with objects, is that array automatically set to nil?

For example:

NSMutableArray *buffetItems = [[NSMutableArray alloc] init];

And would the same apply when you use the class method array?

NSMutableArray *buffetItems = [NSMutableArray array];
4
  • 1
    The array is set to "empty". Commented Feb 4, 2015 at 22:34
  • I was having trouble visualising how exactly 'empty' is represented. Count = zero seems to be it. Commented Feb 4, 2015 at 23:09
  • 1
    You can have a shelf with all your books on it; you can have a shelf which doesn't contain any books; you can have a wall with no shelf. You can have an array that has strings in it; you can have an array which doesn't contain any objects; you can have an array variable with no array. Commented Feb 4, 2015 at 23:23
  • 'array variable with no array': awesome way of encapsulating it. Thanks a ton Josh! P.S I like the carrick bend ;) Commented Feb 4, 2015 at 23:35

1 Answer 1

1

No.

nil and the empty array are completely different things in Objective-C, unlike, say Lisp.

nil is (for all intents and purposes) an opaque value that indicates the absence of any value in a variable. This is fundamentally semantically different than the absence of elements in an array.

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

11 Comments

So when you create an array with alloc and init, what value is it initialised to? Or is it simply allocated a memory address?
@shinnyWhack: the empty array.
@shinnyWhack: the array with no elements in. It.
How is that represented? By 0?
Yes, and no. It has a value - that value is the empty array. Think of it this way: nil is having nothing, and the empty array is having an empty box. You still have something, it just doesn't have anything in it.
|

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.