Preface

Objective-C is verbose. Objective-C is clunky. Objective-C is ugly. I have heard all these things said about Objective-C. On the contrary, I find it elegant, flexible, and beautiful. However, to get it to be these things, you must understand not only the fundamentals but also the quirks, pitfalls, and intricacies: the topic of this book.

About This Book

This book doesn’t teach you the syntax of Objective-C. It is assumed that you know this already. Instead, this book teaches you how to use the language to its full potential to write good code. Objective-C is extremely dynamic, thanks to having its roots in Smalltalk. Much of the work that’s usually done by a compiler in other languages ends up being done by the runtime in Objective-C. This leads to a potential for code to function fine during testing but to break in strange ways later down the line in production, perhaps when processing invalid data. Avoiding these problems by writing good code in the first place is, of course, the best solution.

Many of the topics are not, strictly speaking, related to core Objective-C. Reference is made to items found in system libraries, such as Grand Central Dispatch, which is part of libdispatch. Similarly, many classes from the Foundation framework are referred to, not least the root class, NSObject, because developing with modern Objective-C means developing for Mac OS X or iOS. When developing for either, you will undoubtedly be using the system frameworks, collectively known as Cocoa and Cocoa Touch, respectively.

Since the rise of iOS, developers have been flocking to join the ranks of Objective-C development. Some of these developers are new to programming, some have come from Java or C++ backgrounds, and some have come from web-development backgrounds. In any case, all developers should take the time to learn how to use a language effectively. Doing so will yield code that is more efficient, easier to maintain, and less likely to contain bugs.

Even though I have been writing this book for only around six months, it has been years in the making. I bought an iPod Touch on a whim; then, when the first SDK for it was released, I decided to have a play with development. That led to me build my first “app,” which I released as Subnet Calc, which immediately got many more downloads than I could have imagined. I became certain that my future was in this beautiful language I had come to know. Since then, I have been researching Objective-C, regularly blogging about it on my web site, www.galloway.me.uk/. I am most interested in the inner workings, such as the guts of blocks and how ARC works. When I got the opportunity to write a book about this language, I jumped at the chance.

In order to get the full potential from this book, I encourage you to jump around it, hunting for the topics that are of most interest or relevant to what you’re working on right now. Each item can be read individually, and you can use the cross-references to go to related topics. Each chapter collates items that are related, so you can use the chapter headings to quickly find items relevant to a certain language feature.

Audience for This Book

This book is aimed at developers who wish to further their knowledge of Objective-C and learn to write code that will be maintainable, efficient, and less likely to contain bugs. Even if you are not already an Objective-C developer but come from another object-oriented language, such as Java or C++, you should still be able to learn. In this case, reading about the syntax of Objective-C first would be prudent.

What This Book Covers

It is not the aim of this book to teach the basics of Objective-C, which you can learn from many other books and resources. Instead, this book teaches how to use the language effectively. The book comprises Items, each of which is a bite-sized chunk of information. These Items are logically grouped into topic areas, arranged as follows:

Chapter 1: Accustoming Yourself to Objective-C

Core concepts relating to the language in general are featured here.

Chapter 2: Objects, Messaging, and the Runtime

Important features of any object-oriented language are how objects relate to one another and how they interact. This chapter deals with these features and delves into parts of the runtime.

Chapter 3: Interface and API Design

Code is rarely written once and never reused. Even if it is not released to the wider community, you will likely use your code in more than one project. This chapter explains how to write classes that feel right at home in Objective-C.

Chapter 4: Protocols and Categories

Protocols and categories are both important language features to master. Effective use of them can make your code much easier to read, more maintainable, and less prone to bugs. This chapter helps you achieve mastery.

Chapter 5: Memory Management

Objective-C’s memory-management model uses reference counting, which has long been a sticky point for beginners, especially if they have come from a background of a language that uses a garbage collector. The introduction of Automatic Reference Counting (ARC) has made life easier, but you need to be aware of a lot of important things to ensure that you have a correct object model that doesn’t suffer from leaks. This chapter fosters awareness of common memory-management pitfalls.

Chapter 6: Blocks and Grand Central Dispatch

Blocks are lexical closures for C, introduced by Apple. Blocks are commonly used in Objective-C to achieve what used to involve much boilerplate code and introduced code separation. Grand Central Dispatch (GCD) provides a simple interface to threading. Blocks are seen as GCD tasks that can be executed, perhaps in parallel, depending on system resources. This chapter enables you to make the most from these two core technologies.

Chapter 7: The System Frameworks

You will usually be writing Objective-C code for Mac OS X or iOS. In those cases, you will have the full system frameworks stack at your disposal: Cocoa and Cocoa Touch, respectively. This chapter gives a brief overview of the frameworks and delves into some of their classes.

If you have any questions, comments, or remarks about this book, I encourage you to contact me. You can find my full contact details on the web site for this book at www.effectiveobjectivec.com.