Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMarc Zovighian
1,664 PointsWhat happens when I just use init instead of initWith?
is a default value of nil passed to object? How can I initialise an object without a value?
Thanks for the help
2 Answers
Kevin Hamil
8,203 PointsYes, you can just use init without passing any parameters to it, however, initWith is a better approach because it consolidates additional code that would otherwise need to be written in order to produce the same result.
Hopefully that answers your question... Not sure if you're referring to a more specific situation.
Kevin Hamil
8,203 PointsI'm fairly new to Obj-C myself, so my answer may not be entirely correct... but I think it is.
I wouldn't think that "0" would be stored by default because that's an actual value. I would expect it to just be empty. By allocating the memory space and initializing without a value, you're essentially creating the space for a value to live and generating a pointer to how to get to that space... still no value, just empty.
I decided to run a NSNumber alloc and init without a value and NSLog the pointer to see what it said. The value printed is (null)... so I would say that it's just empty/nil... nothing has been placed there yet and a value is not stored there by default.
Marc Zovighian
1,664 PointsThanks a lot Kevin! Good thinking I should've thought of doing that.
So I guess my next question is : isn't that similar to dynamic memory allocation for NSMutableArrays for example? Since we're effectively allocating some memory for null, what if my number turns out to be a really huge number, we'll have to allocate more space once I assign a value to it?
I just tried out NSLog on sizeof(myNumber), before and after giving it a huge value, and it always seems to return 4 bytes. So I guess this is the size of the pointer to the NSNumber not the size of the actual NSNumber in the heap.
Kevin Hamil
8,203 Points4 bytes is allocated for a long (-/+ 2147483647) and unsigned long (4294967295), so unless the number is just massive there shouldn't be any need to allocate more memory.
Marc Zovighian
1,664 PointsMarc Zovighian
1,664 PointsHey Kevin,
Thanks for your reply.
I was actually referring to a video in the course where the instructor explains how memory is allocated and objects are initialised.
In the video he uses the example of:
NSNumber *myNumber = [[NSNumber alloc] initWithInt:42];
and he explains that first of all memory is allocated for myNumber in the memory grid thanks to the alloc method, and then the object is initialised and the memory block holds 42 as a value for that object.
But if I'm only using init, as opposed to initWithInt, then the object is initialised but with no value. Does this mean that the object is initialised with a value of nil or 0 by default? In other words, memory is allocated but what value does this memory address hold?