Join Simon Allardice for an in-depth discussion in this video Why use Objective-C?, part of Objective-C Essential Training.
One question I get from people new to the Apple developer world is, what's the big deal with Objective-C anyway? Why don't we use a language that's more generic and cross-platform? Perhaps something more widespread, like Java or more modern and fashionable like Ruby or Python? Well, because history, that's why. We have a lot of history and it's important because only through understanding how we got here will some of the things that we do on a day to day basis in Objective-C make a lot more sense. So, super quick history lesson.
We start a little over 40 years ago with tah-dah, the hugely influential C programming language. It's still in use today and many modern languages owing their existence or appearance or at least partially to C. And we jump forward a few years, and the language Smalltalk appears. Not quite as popular but certainly as influential. Now Smalltalk is an object-oriented language, whereas C is not. And in the early 80s, a lot of people were looking for ways to have an object-oriented version of C.
One way was to branch off and create another language like C++, but another was to take some Smalltalk ideas and roll them into C to make a new language. And that's what Objective-C did. Now this is an important distinction. There are quite a few languages, like Java and C#, that are object oriented languages. Heavily based on C, they evolved from C, they took that general format but they were designed as their own independent language. And felt free to depart from C where they see fit.
Now Objective-C is not like that. It wasn't designed ground up as a new language. It isn't influenced by C. It is not inspired by C. Is simply the C programming language with more stuff added to it. It's what's called a strict superset. It is C plus more features. I could copy and paste a bunch of regular C from the 1970's into an Objective-C program today and expect it to compile. I couldn't paste that same C into Java or C# and expect that to work.
Now, why's that important? Well, if you're going to have a new language that simply adds features on top of an older existing one, you're going to need some way to indicate which are the new Objective-C pieces. So we can give the compiler clues by marking these pieces. And that's why you'll see a lot of at signs and square brackets in Objective-C that you wouldn't have in regular C. So if a long-time C programmer read some simple Objective-C code like this, much of it would look identical, the main function, the for loops, the return statements and so on.
But a difference might be something like exception handling code. A lot of languages now have keywords like try and catch and finally. C does not have those words. But Objective-C does. But the way you'd write it is @try and @catch. These are Objective-C keywords mixed in with what looks like regular seam. So we didn't need an at sign in front of the for, or in front of the return statement but we need it in front of try and catch. We need it in front of auto release pool.
We'll see that @ sign in front of the strings. We'll also have the use of NS in many places, like NSLog, NSException. And also these square brackets. We'll see a lot of these. Now sure, square brackets are used in C but typically for arrays. We use square brackets for much more than that in Objective-C so it's another indicator we're doing something a little different, a little special. It's not regular C. And as we go through these movies, you'll see that there are other words, things like @ sign interface, implementation, property, protocol and many more uses of NS words.
Now the downside is, yes you can sometimes see two ways of doing things in the same program. The old C way and the new Objective-C way mixed in with each other. Now okay, that might let you recognize why the syntax can sometimes look a little strange, but it doesn't explain why we still use this language now. So, back to the history lesson. Well, in the 80s, Objective-C, while still a fairly new language was adopted as the primary language of the company, NeXT Computer, which is the one Steve Jobs founded after he left Apple in 85.
And they built everything, including their entire NeXTStep operating system, using Objective-C. And in the mid 90s, Apple bought NeXT Computing, and they rolled the NeXTStep operating system into what would become Mac OS 10. Now, you can see this history even in our first simple project. Remember that NSLog message. Well, NS is from Next Step. Even though the last version of Next Step came out in 1995, the two letters NS will permeate everything we do in Objective-C development.
So Objective-C is not an arbitrary choice of a language. It is a historical necessity. Mac OS 10 is built on over 20 years of layers of Objective-C history and IOS is built on top of Mac OS 10. You cannot pull Objective-C from this picture. Objective-C is woven through Apple development at every level. So although the at signs in square brackets might look a little strange if you're coming from a purer C based language. Hopefully now you know there's a reason they look that way.
- Installing the tools
- Understanding the structure of an Objective-C program
- Logging messages and using format strings
- Writing conditional code
- Using code snippets
- Working with variables, classes, and functions
- Working with objects
- Using existing classes in the Foundation framework
- Managing memory usage
- Creating custom classes
- Working with Objective-C collections
- Reading and writing to files
- Understanding inheritance and NSObject
- Using Categories, Protocols, and Class Extensions
- Compiling and debugging code