0

I have two viewcontroller:

viewcontroller1 - Has Property NSArray array1 and in viewDidLoad I am assigning self.array1 = @[@"a",@"b",@"c",@"d"];

Question 1 : How without coding alloc, this array1 allocated memory and storing values?

Question 2 : Now I have viewcontroller2 - Has Property NSArray array2

Using prepareForSeque method, I am assigning array1 value to array2,

[[segue destinationViewController] setArray2:self.array1];

In the viewcontroller2 - viewDidLoad if I log array2 I am getting the value. Where got array2 memory allocated?

falsarella
  • 12,217
  • 9
  • 69
  • 115
dinesh
  • 72
  • 1
  • 7

1 Answers1

4
  1. its called syntactic sugar, the compiler allocates the needed memory.
  2. array2 is just a pointer. you assign to it the memory address of the allocated memory of array1

you can look here for good examples

Community
  • 1
  • 1
Dima
  • 8,586
  • 4
  • 28
  • 57
  • Hi Dima ,Thanks for the response . Question1 - Got it! Question2 - My understanding reference count of array1 is increased to 2 . The other thing when i dealloc/release array1,then array2 wont have any value. .Please correct me . – dinesh Jun 21 '14 at 14:32
  • 1
    when you release the reference count decreases to 1, so the object is still in the memory. only when you release it again, it will be deleted – Dima Jun 21 '14 at 14:38
  • got doubt here - Ok now the RC for array1 is 2 .When i do dellac the RC becomes 0 not 1 .please correct me . – dinesh Jun 21 '14 at 14:58
  • 2
    Great answer. Just wanted to add that the "syntactic sugar" in this case can be referred to as an NSArray literal. It's a shorthand way of allocating and initializing your instance of NSArray. – user3344977 Jun 22 '14 at 01:16