0

I am some kind of messed up with the following commonly used code :-

NSArray *arrayData = [[NSArray alloc] init];

Problem is someone is telling that arrayData is an object since it is derived from NSArray Collection class but at the some time it is allocated some memory and declared as a pointer variable.

Please clarify whether it's a variable or an object ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
shubham mishra
  • 971
  • 1
  • 13
  • 32

7 Answers7

2

arrayData is a variable. It is a reference (pointer) variable, so it refers something.

What it refers to, the target of the pointer, is an object.

In Objective-C all objects are heap allocated, that means, that they are created solely at runtime in heap memory. In such a case you always need a reference to it, which is typically a variable allocated in stack memory.

Try this:

NSMutableString *string = [NSMutableString new]; // A variable string referring a mutable string object;
NSMutableString *string2 = string; // Another variable string2 referring the same (identical) mutable string object.
[string appendString:@"Amin"]; // The object, string refers to is now "string"
NSLog( @"%@", string2 ); // The same (identical) object, referred by another variable is now "string", too.

To make it more complex, C – the base of Objective-C – calls an "object" everything that lives at runtime: Variables, OOP-objects, …

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
1

In your code snippet arrayData is technically a pointer. It is a pointer to an address in memory whose value contains an object of type NSArray.

Conversationally you can probably get by with referring to it as an "object" but its important to know what is.

Is it a variable?

Its probably also safe to think about the arrayData pointer in your code as a variable that has a scope and that can be manipulated.

You might also be getting tripped up on the -alloc and -init methods..

Very generally speaking...

-alloc: allocates a slot in memory for your NSArray object.

-init: initializes that NSArray object and returns a pointer to that object in memory.

More on working with objects: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html

And Obj-C initialization: https://www.binpress.com/tutorial/objectivec-lesson-11-object-initialization/76

Aaron
  • 7,055
  • 2
  • 38
  • 53
1
NSArray *arrayData = [[NSArray alloc] init];

There's a lot going on in this statement. The first occurrence of 'NSArray' is telling the compiler to create arrayData as a pointer to this type of object (an NSArray), whereas the second occurrence of 'NSArray' (with the alloc) is passing the 'alloc' message (method) to the NSArray class (i.e. Calling the alloc method). Same word, two completely different meanings.

Conceptually, the first thing that happens is [NSArray alloc] which is calling the (class) method 'alloc' in the 'NSArray' class. This returns an (empty) instance of an NSArray object. We the call the 'init' method in our new, empty object. A pointer to the resultant object is then stored in your 'arrayData' variable, and we tell the compiler that this is a pointer to an NSArray object.

Think of it like this:

NSArray *arrayData;         Create a pointer to an NSArray object.
newArray = [NSArray alloc]; Create an empty NSArray object by calling the class method alloc in NSArray 
[newArray init];              Initialise it by calling the object's 'init' method
arrayData = *newArray;        Assign a pointer to it to my variable.
Steve Ives
  • 7,894
  • 3
  • 24
  • 55
1
  1. It's create object of type array in heap memory. and it's reference created in in stack memory. in your case arrayData is reference stored in stack memory which points your array object in heap memory.

  2. arrayData is a variable. It is a reference (pointer) variable, so it refers something.

  3. What it refers to, the target of the pointer, is an object.

0

What is actually happens when you declares NSArray *arrayData = [[NSArray alloc] init];,

It's create object of type array in heap memory. and it's reference created in in stack memory. in your case arrayData is reference stored in stack memory which points your array object in heap memory.

The Object is the instance itself, whereas the Object Variable is the reference to the Object. Here's a contrived example: Object o = new Object(); Object ref1 = o; In his case, there is a single instance of the Object, but it is referenced by two Object Variables: o and ref1

This is the general scenario for object oriented programming and Objective C definitely follows it.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0

In programming, a variable is a name given to a memory location which holds a value, but where the exact value can change over the life of the program. (If the value can't change, due to language constraints, it's called a constant. If it could change, but doesn't, it's still a variable.)

An object is not a variable, though it usually contains a few. The pointer to the object is usually a variable, in that the memory location which contains the pointer to the object may change values over time. From the programmer's perspective, this is using the same pointer to refer to different objects over the course of the program.

Avi
  • 7,469
  • 2
  • 21
  • 22
-1

NSArray is a class. Here:

  • NSArray * - type (From left one)
  • arrayData - variable name
  • [[NSArray alloc] init] - class NSarray, An invocation of a class method

When ever you create any object using alloc/init, its retain count will be increase by one. That means it will occupy some memory. So for memory management you have to take care of that retain count.

sunshine
  • 21
  • 5