Saturday, December 31, 2011

Learning Objective C - Part1

Now that I got a Mac, this holiday season, I decided to start working on learning Objective-C. Objective-C is the programming language used in almost everything Apple - including iPhone and iPad development. It is also used in developing Mac native applications. Working on a system, which I don't fully understand, or can develop with, always bothers me, and hence, all the above reasons combined, I start my Objective-C. Right off the bat, it became clear that, this is a totally different beast in terms of syntax. My C, C++ knowledge will surely come handy here, but at the same time, there are so many things that are being done in Objective-C in a totally different way.

As I proceed through Objective-C, I will be using my Blog to document what I would like to refer back.

 

Variable Declarations in Objective-C


Simple variables are declared just like in traditional C. However, when it comes to object declarations, things are different in Objective-C.

Example of simple variable declaration:

int testInteger = 0;

Not a big surprise here, as objective-C has its roots in traditional C language.


Example of declaring a string


NSString *testString;

The object declarations in Objective-C is always with a pointer. We then allocate and initialize this memory location, and gets the reference of the memory location to the pointer variable. More on memory allocation and freeing, later.

By the way... almost all the standard classes in Objective-C start with "NS".... because if NEST history of apple.


Example of declaring a Custom Class (say... Dog class)

Dog *pluto = [Dog new];

The above line of code declares a pointer of name "pluto". This pointer points to a memory location with some space reserved for instance of the object "Dog". The "new" keyword allocates AND initializes the memory location at the same time.


Another (More Commonly used) example of declaring a custom class

Dog *pluto = [[Dog alloc] init];

The above line of code also initializes a memory location. This is done by the "alloc" section of the code. The square brackets tells XCode that this is Objective-C code. Once allocation is done, the result is passed out, but right into the hands of init method. The job of init method is to initialize the memory location. Once the memory is initialized, it returns the pointer to the memory location out , which promptly gets assigned to the pointer variable "*pluto".


Memory Allocation and De-Allocation


What is Memory Leak

Unlike many modern programming languages like Java, C#...etc, Objective-C does NOT have an in-build garbage collector (There is something called Automatic Reference Counter, or ARC in objective-C. We will get to this sometime later). This means that, we are responsible for cleaning up and freeing memory after its use. If we don't do this, the application will gradually eat away available memory - until no more memory is available.  This is called "Memory Leak" - Pretty bad situation to be in, for mobile devices, and a guarentee for 1 star comments at app store... that is, if the application ever makes it to the app market.

Difference between using "new" and "alloc-init" combination to initialize objects

In the above example, we saw 2 ways of instantiating the “Dog” class. One used the “new” keyword and, the other, 2 keywords, namely “alloc” and “init”. Surely, the second option seemed more complex and unnecessary, as we have a one-step way of doing this.

The reason why the second approach is more desired, is because of the different options we have to initialize the memory location. It gives us finer control on initialization, before allocating the location information to pointer variable.

Instead of “init”, we can use many other “specialized” initializations, with parameters, as shown. This is not possible when using “new”.

image

All right.. 2:25AM now… that’s it for part 1. This was primarily capturing whatever I learned till now. There is some more… and it wil be captured in this/next blog. If someone stumbles across this blog, and find it useful, please leave a comment.