Chapter 5. Basic Objective-C

When I was first learning Cocoa, some very smart people told me that Objective-C is C with objects. I hear that now and it sounds like describing the desert as “a place with a lot of sand.” It’s technically a true statement, but it doesn’t really tell you much about the language.

Writing a Cocoa app is nothing like writing software with standard C libraries. The C foundation is just a vehicle, not the basis for how you design your application. In my experience, there are three standout features of Objective-C:

Compiled speed, dynamic features

Objective-C has the compiled speed of C with many of the features of a dynamic scripting language. You can use static typing for variables if you want, but the types are not strictly enforced. You can also load classes and methods as the application is running, or generate them on the fly.

Compatibility with C and C++

You can freely use C types and libraries within Objective-C classes, as well as integrate with existing C++ code. In fact, some frameworks in Mac OS X are written in C.

Cocoa integration

Cocoa is designed for Objective-C, and Objective-C is evolving around Cocoa. You have other options for languages, but in my opinion, the two are much more powerful together than they are apart.

The first two points are interesting, but the most important feature of Objective-C is its tight integration with Cocoa. The majority of Mac and iPhone apps are written in Objective-C, including Xcode itself. Cocoa apps can be written ...

Get Cocoa and Objective-C: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.