Tag Archives: Effective Objective-c 2.0

Item 3, Prefer Literal Syntax over the Equivalent Methods

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

Prefer Literal Syntax over the Equivalent Methods

Literals have been around objective-c since a long time:

NSString *aString = @"Hello World";

With the latest versions of Clang, this sugar syntax has been extended to NSNumbers, NSArrays & NSDictionaries. Since we deal with this kind of objects every day, this new feature is well appreciated. In a nutshell this allow us to:

  • Reduce the boilerplate
  • Cleaner code
  • Faster typing

While using NSNumbers, you can use literals in the following way:

  • @15 => [NSNumber numberWithInt:15]
  • @15.6 =>[NSNumber numberWithFloat:15.6]
  • @15.123123 => [NSNumber numberWithDouble:15.123123]
  • @YES => [NSNumber numberWithBool:YES]

As you can see, in this case, the amount of extra code is reduced in more than 75%.

You can also use literals in collections with subscripting. With NSArray you can do the following:

  • @[@"One", @"Two"] => [NSArray arrayWithObjects:@"One",@"Two"];
  • By Array subscripting: myArray[1] => [myArray objectAtIndex:1];

And finally with a NSDictionary:

  • @{@"key1":@"object1", @"key2":@"object2"} => [NSDictionary dictionaryWithObjectsAndKeys:@"object1",@"key1",@"object2",@"key2" nil];
  • myDictionary[@"key1"] = @"newObject1"; => [dic setObject:@"newObject1" forKey:@"key1"];

And if you want a mutable object:

  • [@[@"1",@"2" ] mutableCopy];
  • [@{@"key1":@"object1"}mutableCopy];

Although with collections the end result seems the same, if an object is nil, it won’t. While using the common operations adding a nil wouldn’t crash anything, with literals it will:

  • [NSArray arrayWithObjects:@"object1",nil, @"object2", nil] => It’s “ok”.
  • @[@"object1",nil, @"object2"] => It will raise an exception.

In theory it would be better if the first option would crash, because it doesn’t make much sense to have a nil in the middle of an array and work with it in that state. Most of the time, you would prefer that would crash, so you could see immediately where the problem is.

Literals are an awesome addition in a very verbose language like objective-c. Knowing how to use them and their caveats is a must have for every programmer.


Tagged , , , , , , , , ,

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.

Tagged , , , , , , ,

Effective Objective-c 2.0, by Matt Galloway

Over the next weeks, I will be making a small comment about the new book of Mr. Matt Galloway. This comments will be about each chapter’s item, so we are talking about 52 small posts. I prefer to do so, instead of dissecting 7 chapters. So godspeed!

The goal here is not to, in no way, transcript the book, but to be a guide for me, to study and retain what the book is trying to transmit.

Tagged , , , , , ,