Suppose I have the following class definition
@interface ClassX: NSObject
@property NSArray *arr;
ClassX *obj = [ClassX new];
obj.arr = [NSArray arrayWithObjects:[NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil]; //Edit to avoid getting derailed by differences due to @ notation
NSNumber objects are heap-allocated, not stack-allocated.
They do not look like heap objects because compiler hides it from you using the new relatively syntax with
When you write
The compiler translates it to
[NSArray arrayWithObjects: [NSNumber numberWithInt:1], [NSNumber numberWithInt:2]]
The end result is as follows: the stack has a pointer to an object of type
ClassX. The memory pointed to by this pointer starts with an isapointer, followed by a pointer to another location in the heap that has
NSArrray object in it. In turn, this part of heap memory has another isapointer followed by a pointer to a block of memory representing the inner array of
NSArray, containing two pointers to
NSNUmber objects. Each of
NSNumber objects has an isapointer followed by data which represents the number.