Book Review: Effective Objective-C 2.0 by Matt Galloway

I love reading technical books. They provide you with a broader picture on a topic and go in depth on different issues that online tutorials just simply skim. That’s why each month I pick up a book related to the platforms I work with. My last month’s pick was “Effective Objective-C 2.0” by Matt Galloway. It was an enlightening read, so I thought I would share a review with my blog readers.

effective-objc-2.0

If you are like me, you picked Objective-C as a language while learning iOS development. I have over the years picked and improved my know-how of the language that is Objective-C, while being a full time iOS developer. The problem with learning Obj-C as you learn iOS development is that you are for the most part learning only higher level Cocoa (Touch) APIs and hardly get to see the efficient implementation behind these APIs on the language level. Thus I think it is important to set aside sometime, give Cocoa a break, and play with Obj-C language itself and get to know how it works under the hood, many of the new improvements it has to offer since 2.0 version, and some of the new ways the new Apple compiler, LLVM, has improved on it.

“Effective Objective-C 2.0” provides you with a clear path to knowing your programming language. It is divided into 52 items touching different parts of the language, divided into seven chapters.

Chapter 1: Accustoming Yourself to Objective-C

A simple start to the book, introducing you to Objective-C’s roots, how headers import work and how to effectively use it. Also touches on literal syntax, macros, and enumerations.

Chapter 2: Objects, Messaging, and the Runtime

Many newcomers to the language will go a long way working with Cocoa touch without realising that Objective-C has a very powerful runtime whose power can be harnessed to perform many useful tricks. This chapter talks about properties, objects equality, associating custom data to existing classes during runtime, talks about objc_msgSend and how message forwarding works. It also touches upon the very powerful Obj-C runtime feature of method swizzling.

Chapter 3: Interface and API Design

This chapter talks about how to properly implement your classes with clear and useful interfaces.

Chapter 4: Protocols and Categories

Protocols and Categories are prevalent throughout the Cocoa framework. This chapter talks about effectively using categories to provide additional functionality on top of your classes.

Chapter 5: Memory Management

Many people think that after introduction of ARC, studying memory management on iOS is not important. Nothing can be far from the truth. Understanding how memory management works on the platform is essential to understanding how ARC works and to avoid some of the gotchas ARC can have if you don’t understand how to avoid retain cycles even when ARC is enabled.

Chapter 6: Blocks and Grand Central Dispatch

One of the modern features of Obj-C are Blocks. This chapter talks about how to effectively use blocks. It also talks about the C API to handle concurrency on platform namely GCD and compares it to Operation Queues. It talks about how one can use GCD to allow thread synchronisation, instead of the @synthesize construct that most of us are used to.

Chapter 7: The System Frameworks

Last chapter talks about features provided in system frameworks and when should they be preferred. For example using block enumerations to for loops, using toll free bridging for memory management, using NSCache instead of NSDictionary to avail caching etc.

Conclusion:

This is a great book to have read. It will certainly make you more aware of what goes under the hood when you use any of the system libraries, and put you in a better position to write better classes of your own and debug like a pro.

UIView bounds vs. frame

New comers to iOS development usually get confused by a UIView having two different properties to represent its position and size. Namely ‘bounds’ and ‘frame’. Surprisingly, many senior iOS devs can’t explain in clear terms the difference between the two either. So I thought I would try to explain in simpler words the difference between the two.

UIView — An infinite sheet:

An instance of UIView always has a height, and a width. But to clearly understand the difference between bounds and frame, we first need to revisit our visualisation of a UIView instance. Think of a UIView as an infinite sheet, that is divided into four quadrants, just like a cartesian coordinate plane. However, a regular cartesian coordinate plane has its y-axes increasing towards the top. While our UIView plane’s y-axes increases downwards, and has negative values upwards. Just like a regular cartesian plane, our UIView plane has its centre at point (0, 0).

Even though a UIView can be thought of as infinite sheet, and we can add subviews to it anywhere in this infinite space, we only get to see a finite section of this infinite sheet at a given time. And this is where bounds and frame come into play.

Bounds:

Bounds define a finite window into the infinite sheet of UIView. At any given time, we only see the contents of the view that lie within the bounds of this view. Any content that is part of the view, but lying outside the bounds won’t appear (or would appear if it is within the bounds of the superview and view doesn’t have its clipsToBounds set to YES, but won’t receive touches). If we change the position and size of bounds to move to another portion of the infinite sheet of view, content at that location would start to appear.

Thus, bounds is a window into an infinite sheet of content that is UIView.

Frame:

Frame is the location and size of a UIView instance within its superview. Thus if we don’t change frame, but change bounds, view would show different contents, but at the same location within the superview. If we change frame, and keep bounds the same, view would show the same content but at a different location within the superview.

Remember that frame and bounds have same size values, but their origin values can differ.

Conclusion:

Bounds is view’s location and size within its own coordinate system. Frame is views location and size within superview’s coordinate system.

 

SKHeap; A quick MaxHeap and HeapSort implementation

Heap is an interesting data structure. It is a binary tree, that keeps the maximum (in case of MaxHeap) element at the root, thus providing access to it in O(1) i.e. constant time. You can pop the root element in O(lgN) time, and insert new elements into the heap in O(lgN) time as well. Initial buildup of the heap takes linear time. You can sort a sequence of elements using heap, by first building the heap out of all the elements, and then retrieve the elements back. Since initial build up is linear time, each retrieval of the maximum element is O(lgN) time, and you do N such retrievals, the resulting sorting algorithm is of O(NlgN) complexity and called HeapSort.

It is fun to implement these well known data structures and algorithms. So while I was bored and free for half an hour, I quickly wrote a basic implementation of a Heap and HeapSort in Objective-C and pushed it to Github. Check it out and let me know if you like it or find any issues with it.