It’s time to let the cat out of the bag.
I’m writing a book that explains iOS programming to .NET developers! I have written seven of the twelve chapters and, I must say, it’s really coming together nicely so far. This book will be a companion manual for any .NET developer looking to make the leap into iOS programming. Considering the explosive growth in demand for iOS developers these days, I think a lot of people might benefit from a book like this.
I’m writing the book I wished for when I started teaching myself native iOS programming. It explains the terms and concepts involved with iOS programming by relating them to your knowledge of .NET development.
Here is a sneak peek from Chapter 4, titled ‘From C# to Objective-C’…
Reference vs. Pointer
Every object in a program exists at a unique memory address. Most of the time C# code stores the location of an object as a variable in the form of a reference. In contrast, Objective-C stores the memory address as a variable in the form of a pointer. C# also supports pointers, but only in blocks of code explicitly marked as ‘unsafe.’ Objective-C only supports pointers.
In iOS software every pointer consumes four bytes of memory, according to the sizeof() operator. Those bytes store a number. That number happens to be a memory address of either zero (which is known as nil) or an address at which a data object should exist. Note that I did not say a data object must exist at the address stored in a non-nil pointer. This is the fundamental difference between pointers and references. Pointers are more flexible than references, but less safe.
Another way to think about the difference between references and pointers is to consider how they are used. In .NET programming there is a garbage collector (GC) that removes unreferenced objects from memory. The GC also moves objects around on the heap (the section of memory where objects are allocated) in order to create contiguous blocks of available memory, making subsequent allocations faster. The fact that objects are moving around on the heap is completely irrelevant to C# code that only uses references to access those objects. The .NET runtime takes care of adjusting references to ensure they point to relocated objects. A reference points to an object, regardless of where it currently happens to be in memory.
Pointers, on the other hand, point to a specific location in an application’s memory address space. If an object is moved, pointers to the object’s original memory address are not automatically updated with the new address. However, since the Objective-C runtime on iOS does not have a garbage collector, and objects do not move around in memory on their own, this is not as much of a problem as one might think. The additional burden imposed by pointers on application developers is partially mitigated by Automatic Reference Counting, which I review in Chapter 5. Also, what is lost in safety and convenience by not having references is compensated by the flexibility of using pointers.
Stay tuned for more…
The book has been published! Please visit this page for more information.