Item 1, Familiarize Yourself with Objective-C’s Roots

Note: This post is part 1 of a series of posts I will do about the new book of Matt Galloway.

Familiarize Yourself with Objective-C’s Roots

Dynamic language

In this item, the author starts by delving into how Objective-c send messages as opposed of function calling (in this case comparing to C++)  and when one should be used instead of the other (you can still use function calling in objective-c).  The dynamic nature of objective-c, binds the messaging to the corresponding piece of code that will be called, at runtime; as opposed to C++ (for example) that is done at compile time.

Memory Management

One should understand how related objective-c is to c (it’s a superset) and how/where the allocations are made:

NSString *aString = @"BlahBlah";

In this case “aString” is a pointer to a piece of memory allocated on the heap. Although this is mostly true for allocating objects in objective-c, blocks for example are allocated on the stack, but can be  passed to the heap (block_copy()).  An excellent post by Mike Ashe explains this as well.  Plus when you alloc, new, retain or copy an object, you are responsible for them.  It’s important as well to know how the memory is managed in objective-c:

 ” (..) that any single object can have multiple “owners”, and the system won’t allow the object to be destroyed until all owners have relinquished ownership.” – Mike Ashe,  Stack and Heap Objects in Objective-C

In one hand with manual memory management, you are responsible for cleaning the house, with ARC the cleanup is being made for you. Do remember, that ARC is not a Garbage collector, so you should need to understand when you have to do something (circular references for example) and when you don’t.

You will find plenty of variables that will be create on the stack, for example:

CGPoint point = CGPointMake(12.0f,10.0f);

CGPoint is a structure and it’s defined like this on CGGeometry.h from CoreGraphycs.framework:

struct CGPoint {

CGFloat x;

CGFloat y;

};
typedef struct CGPoint CGPoint;

It’s important to measure if you are going to use objects (vars that point to something that has being allocated on the heap) and, in this case, structures (vars that might be using stack space), or non-objects int,float, char, etc. Allocating objects  can incur on an unnecessary overhead, so chose the right tool for the job in hand.

The first item, is a very well written intro into how objective-c works internally versus other languages, and how you can take advantage of the C language . As it is build upon it, some time should be taken to understand it.

Advertisements
Tagged , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: