How do you develop apps for iOS? It’s a common question, and one we hope to answer with this series of posts.
Learning how to develop software can be one of the most intimidating prospects for any computer enthusiast, and with the growing saturation of applications in mobile marketplaces, it is becoming increasingly difficult to get your work noticed. That’s what this series is for, helping you learn iOS development from a conceptual perspective. No prior knowledge of computer programming will be necessary. Over the coming weeks, we’ll examine the iPhone’s ability to deliver immersive, intuitive content, a unique opportunity for both developers and consumers.
Today, we’ll be diving into the history and use of Objective-C 2.0, the language that is primarily used in developing for iOS devices.
A Brief History of Objective-C
Created primarily by Brad Cox and Tom Love, both employees of Stepstone, Objective-C can be traced back to the early 1980s. Building upon Smalltalk, one of the first object-oriented language, Cox’s fascination with problems of reusability in software design and programming resulted in the creation of the language. Recognizing that compatibility with C was crucial to the success of the project, Cox began writing a pre-processor for C to add backward compatibility with C, which soon grew into an object-oriented extension to the C language.
Cox showed that the construction of interchangeable software components really only needed a few practical changes to existing conventions. Objects needed to be supported in a more flexible manner, which would work in conjunction with a usable set of libraries, allowing for code to be bundled into a single cross-platform format.
To commercialize their creation, the dynamic duo created Productivity Products International, which allowed for the sale of an Objective-C compiler with class libraries. In 1986, Cox published a book about the language entitled Object-Oriented Programming, An Evolutionary Approach. Although the main focus of the instructional text was to point out the issue of reusability, Objective-C has been compared feature-for-feature with the major players in the programming game every since.
After Steve Jobs’ departure from Apple, he started a new company called NeXT. In 1988, NeXT licensed Objective-C from the owner of the trademark, releasing its own Objective-C compiler and libraries on which the NeXTstep UI and interface builder were based. The innovative nature of this graphics-based interface creation resulted in the creation of the first web browser on a NeXTstep system.
With Apple’s acquisition of NeXT in 1996, Apple used OpenStep, the standard that Steve Jobs pushed forward based on the Objective-C libraries he so vehemently supported, to build Mac OS X. This included a new developer tool that was later replaced by Xcode as well as a design tool called Interface Builder. Most of Apple’s present-day Cocoa API is based on NeXTstep interface objects.
At the Worldwide Developers Conference in 2006, Apple announced Objective-C 2.0, a revision of Objective-C that included syntax enhancements, performance improvements, and 64-bit support. Mac OS X officially included a 2.0-enabled compiler in October 2007. It is unclear at this time whether these language changes will be available in the GNU runtime, or if they will be implemented to be compliant with the larger Objective-C 2.0 standard.
What Differentiates Objective-C from Other C Languages?
Objective-C is essentially an extremely thin layer on top of C, making it a superset of the language. Moreover, it is possible to compile any C program with an Objective-C compiler, including C code within an Objective-C class. All non-object-oriented syntax operations are derived from C, while the object-orientation features an implementation akin to Smalltalk messaging.
To help you understand the finer details of Objective-C, we have listed a few of the unique characteristics below with definitions and examples to be used at your leisure.
Messages – Object-oriented programming is based on message passing to object instances. Objective-C does not call methods, but rather will send a message. The target of the message is resolved at runtime, with the receiving object interpreting the message. The object to which the message is directed is not guaranteed to respond to a message, and if it does not, it simply raises an exception. Using this Smalltalk style messaging allows for messages to go unimplemented, with the method resolved to the implementation at runtime. Due to the fact that these messages are interpreted at runtime, Objective-C messaging takes three times as long as the traditional C++ virtual method. However, subsequent operations are completed twice as fast.
Interfaces and Implementations – Objective-C requires that the implementation of a class be in separately declared code blocks, which has led developers to the convention to place interface in the .h header file and implementation in suffixed with .m, similar to those found in C.
The interface class denotes class methods, which can be called with out an instance of the class, with plus signs, and minus signs denote instance methods, which can only be called within a particular instance of the class. Methods have no access to instance variables, resulting in code equivalent to C++ interfaces.
The implementation method is found within files designated with the suffix .m, which are written using interface declarations. Internal representations of a method vary between different implementations, but the internal names of the function are rarely used directly. Messages are converted to function calls defined in the Objective-C runtime library.
Dynamic Typing – Like Smalltalk, Objective-C can send a message to an object that is not specified in the interface, allowing for increased flexibility. This behavior is known as message forwarding and sometimes delegation. An error can be used in case the message cannot be forwarded and messages that are never received can silently drop into the background. Static typing information can be optionally added to variables, which is checked during compilation.
Instantiation – Once a class is written, it can be instantiated, meaning that a copy of an object is captured in an instance, which share the same attributes, but the specific information within these attributes differ. An example of this would be if you were to create a class called “Employee,” describing all attributes common to employees. They all have names and salaries, but they differ in the numerous cases that will show up.
Memory must first be allocated to the new object and then initialized, and it is not fully functional until both of these steps are performed.
Forwarding – Objective-C permits the sending of a message to an object that may not respond. Rather than dropping the message, an object can forward the message to an object that can respond. It can be done through the simple implementation of certain design patterns. An object wishing to implement forwarding needs only to override the forwarding method with a new defining method.
Categories – One of the main concerns that the developers behind Objective-C battled with was the maintainability of large code bases. Borrowing from Smalltalk, Objective-C extended the concept of categories to help with this process. It collects method implementations into separate files, placing groups of related methods into a category to make them more readable. While we will not go into the technical aspects of this now, Objective-C’s categories build upon the standards dictated by other programming languages.
Is Objective-C the Best Programming Language?
Objective-C is not necessarily the best programming language considering its numerous flaws. Its implementations use runtimes written in C, which adds size to the application. An additional reason that programs tend to be bloated is the fact that Objective-C’s dynamic typic does not allow methods to be stripped. However, the fact that Objective-C draws from the foundation that C already has makes it incredibly useful application of object-oriented programming.
Feel free to ask questions about this lesson in the comments section below and we’ll do our best to address your concerns.
To read previous installments from this series, check the links below.
- Introduction to iOS Development: Understanding Your Platform
- Introduction to iOS Development: Installing Xcode
- Introduction to iOS Development: Creating an Intuitive User
- Introduction to iOS Development: Taking Advantage of the iPad 2
- Introduction to iOS Development: Coding Your First Application