Pointers

In Objective C all objects are referenced using pointers:

A pointer is a variable which stores memory address instead of value itself.

how big is int, 4 bytes
how big is char, 1 byte
how big is String, image or video?  we often don’t know and it might even change during our program so we need more flexible way to manage objects and pointers are one way of allowing us to do that.

Using pointers allows us to pass around the objects (just by passing around the address of object) without having need to copy them which is time consuming and inefficient  

E.g.   NSString* welcomeString;
         NSDate* todayDate;

welcomeString = @“Welcome to Objective C”;

Here the string 'Welcome to Objective C’ string is not directly stored in ‘welcomeString’ instead the address/memory location of 'Welcome to Objective C' is stored in ‘welcomeString which is contrary to 'int highScore = 100’ where ‘highScore’ variable directly stores value. ( this is because objects are more complex than primitive types)

if you want to make and use of simple object, we need pointers to reference them.
Note: Explicit use of pointers in Swift is not required unlike Objective C. e.g let someInstance = SomeClass ( ) whereas in objective C it is SomeClass *someInstance = [SomeClass alloc] init];

No comments:

Post a Comment