I recently started taking a class on Software Development for Mobile devices.
Naturally we started with iOS development, but I was surprised to find myself appalled at the look and feel of Objective-C. After all, I love all of my Apple products. What the heck is up with this hideous language?
Over time, Objective-C has grown on me, and I thought I’d take a few minutes to write down some things I wish I had known before diving in to Objective-C!
1. What’s with all the weird symbols?
Long names, asterisks, brackets, @ signs, weird prefix letters like NS all over the place, colons in the middle of function calls…
Yeah, it makes you cringe at first, but it’s not so bad once you get used to it. Here’s a quick reference:
[ ] : Brackets are used to call a function, or “send a message” as it’s called in Objective-C.
For example, let’s say “jon” and “kelly” are instances of a person class. Printing the person’s name might look like this: [jon printName]. Functions can also be nested, like this:
[jon getAgeDifference: [kelly age]]
which would first retrieve kelly’s age, and then send it as an argument to the getAgeDifference function called by jon.
@ : The “at” sign is used with the string object.
* : The asterisk symbol simply means you are dealing with a pointer.
In Objective-C, you will see a lot of these all over the place – basically any time you have a variable which refers to an object. (Pretty much anything but ints, floats, chars and bools).
NS : There’s really no reason to be put off by the letters NS. They stand for Next Step, and are part of Apple’s history with buying other companies. NS is just something you will see all over the place and shouldn’t worry about.
2. Objective-C is not a C-based language like C++.
Objective-C is just classic C with really weird-looking object oriented stuff smashed on top. This means that you could put C code into XCode, and it would run. (If you see some Objective-C code that triggers your gag reflex, it’s probably object-related).
3. Forget all the OOP syntax you’re used to.
Yes, some “normal” syntax has been introduced into Obj-C, such as dot methods, but you should learn the odd-looking Objective-C way of doing things, because that’s what you will see in the wild, and that’s how you’ll be taken seriously as an iOS or OSX developer.
4. Printing to the console looks weird.
Let’s just print an integer to the screen. In C++, we have:
cout << a;
In Objective-C, we would have:
Yep, pretty awful. Did you die a little inside? That means we’re dealing with Objects! (Yes, strings are objects in Objective-C, unlike ints and floats, which are primitive types).
So for writing to the console, just remember, NSLog(@”insert something here”); is the basis.
Then inside the quotes you can either put letters you want to print, like “Hello World”, or if you want to print data, you need to put placeholders using the % sign for any variables. In the above example, %i is used as a placeholder for the integer a.
Here’s a list of format specifiers from the Mac Developer Library.
5. Naming functions is weird.
In Objective C, the arguments passed into a function are written into the name. That probably sounded harmless because you weren’t paying attention.
The arguments passed into a function are part of the function name!
Okay so a “normal” function that takes two arguments would look like this:
void printName(string firstname, string lastname);
In Objective-C, we would write it like this:
-(void) printFirstName: (NSString *) firstName andLastName: (NSString *) lastName;
Okay, yeah, it’s long and weird and you hate it. But before you object, there’s something really cool about this way of doing things. There is absolutely no confusion about which argument applies to which parameter. That’s especially useful in cases where you have 4 or 5 arguments being passed in, or when the parameters are given unintuitive or abbreviated names.
Let’s imagine we are creating a function which prints six facts about a person. In C++, we have:
void print(string f, string l, int a, string g, int s, int i);
If we wanted to call this function, we would have something like this:
print("Kelly", "King", 26, "female", 55000, 21);
So when you’re writing that function call, you need to have your function definition handy, or you won’t know what order to enter your info. Additionally, you may not remember what each letter stands for. In the above example, “i” was meant to represent ID, though it was unclear from the function prototype.
Now let’s look at the Objective-C way:
void printWithFirstName: (NSString *) f andLastName: (NSString *) l andAge: (int) a andGender: (NSString *) g andSalary: (int) s andID: (int) i;
Already, it’s pretty clear what goes where. You may say “That’s way too long to remember or type.” But you have XCode, which will auto-complete after just a few key strokes. Then you can just hit tab six times, filling in the appropriate detail based on your oh-so-semantically defined function!
6. Creating classes is weird.
I’m not going to get into classes here, because that’s a big old can of worms. I will however, recommend Simon Allardice’s Lynda Course on Objective-C. It’s phenomenal. I spent the first four weeks of my iOS college class beating my head against a wall trying to keep up with assignments. Simon’s Lynda course is only ~6 hours and taught me more than I learned all semester. It’s awesome.
7. ‘Option’-Click is your friend.
If you’re programming Objective-C, you’re probably using XCode. One of the great things about writing for Apple products is that you get Apple’s usual “everything-we-make-is-compatible-and-standardized” guarantee.
If you are in XCode and see something you don’t understand or recognize, just hold the option key and click a word. You will be provided with a snippet about whatever class or method you clicked, and a link to the full documentation (which is honestly pretty easy to read).
Alright, that’s all I have. Do you have any tips you wish you had known when you started learning Objective-C? Share the wealth! Put them in the comments below!