For many developers with a background in Ruby or Python (or other similarly human-readable languages), the switch to Objective-C can be overwhelming because of the differences in syntax. Objective-C code tends to include non-alphanumeric characters, which are confusing to read if you’re not used to seeing them in your code. I’ll go through a few them here, to act as a small Rosetta Stone (Pebble?) as you start reading + writing Objective-C.
- @ – The @ sign is frequently found as a prefix to keywords (@implementation, @interface, @protocol, @end) that are used for defining classes. It’s a direction for the compiler – but as a programmer, it indicates to you where the implementation of your class (or its interface, or protocol) begins and ends.
The @ sign is also found in front of numbers, strings, arrays, and dictionaries. It’s shorthand for creating these objects – consider the snippet below:
NSString *foo = [NSString stringWithUTF8String:"Hello World!"]; NSNumber *bar = [NSNumber numberWithFloat:3.14159]; NSArray *baz = [NSArray arrayWithObjects:foo, bar, nil]; NSDictionary *dict = [NSDictionary dictionaryWithObject:bar forKey:foo];
This verbose syntax is an older way of writing Objective-C. It’s still valid, but the @ sign allows us to simplify it considerably to the snippet below. In this case, the @ sign is also a compiler directive.
NSString *foo = @"Hello World!"; NSNumber *bar = @3.14159; NSArray *baz = @[foo, bar]; NSDictionary *dict = @{foo: bar};
If you want to learn more about the @ sign, the Clang (Objective-C compiler) documentation has technical details. Additionally, Mattt Thompson has a great blog post which goes into some of the more obscure uses of the @ sign.
-
* – The asterisk is literally ALL over the place. An asterisk before a variable definition indicates that the variable is a pointer to an object. For example, let’s consider the first line of code above:
NSString *foo = [NSString stringWithUTF8String:"Hello World!"];
We’re defining
foo
as a pointer to an object of typeNSString.
Most variables you define in Objective-C will be pointers. Exceptions to this rule include int, float, other basic scalar types, and anything whose underlying implementation is a struct (ie, CGRect).When you want to send a message to an object, you do not need to prefix the variable that points to it with an asterisk. Let’s say we want an uppercase “Hello World!” string – we do this by calling
[foo uppercaseString]
, not[*foo uppercaseString]
. - & – The ampersand is the ying to the asterisk’s yang. It returns the address that a variable points to, which is the address at which the object is actually stored. The expression
&foo
might evaluate to0x1a815f0
. Consider the snippet below:NSLog(@"%p", &foo); //logs 0x1a815f0, the address to which foo points NSLog(@"%p", foo); //logs the address that foo itself is stored at
Ampersands are often used to pass around errors. In the code snippet below, the method on NSString modifies our error for us, but does not return a new error.
NSError *error = nil; NSString *fileContents = [NSString stringWithContentsOfFile:@"/path/to/file.txt" encoding:NSUTF8StringEncoding error:&error]; if (error) { //handle the error. It's been changed from nil to something significant. } else { //handle the success! }
You probably won’t see as many ampersands as you will asterisks, but it’s helpful to know what they mean. If you see a method definition with a parameter whose type includes
**
, that’s an indication to pass in the address of the object, rather than the pointer to the object. - # – The pound/hash sign is called a pragma in Objective-C. Like the @ sign, it’s a compiler directive. XCode also understand them, so you’ll frequently see them used for file organization. You’ll frequently see them followed by the words “pragma mark – …”, like below:
#pragma mark - Helper methods - (void)askForHelp { ... } - (void)helpSomeone { ... } #pragma mark - Action methods - (void)didTapBigButton { ... } - (void)didTapSmallButton { ... }
These make it easy for the programmer to organize their methods in a readable way. XCode also creates shortcuts to jump to these sections of the file!
Knowing these symbols, you should be able to read and understand Objective-C more easily!