In this part 2 of Objective-C for Flash developers, we will look at header files and how variables are defined in comparison to ActionScript 3. Before we start, I need to clarify one thing: I will consistently use the term function throughout this tutorial, although technically there is a semantic difference between funcitons and methods. It’s just to make this all easier on the brain, that’s all.
Header files (i.e. MyClass.h)
Any and all Objective-C classes are split into a .h file and a .m file. The former being the header file, the latter the implementation.
This is a concept that has no counterpart in AS3 so a direct comparison won’t be possible. What a header file does is list the definition of your class. In one way you could say that it’s a bit like the part you put before the constructor in you AS3 classes, in another you could say it’s like and AS3 interface. Both are correct to some extent, however neither would be accurate.
If you cmd+click the name of a framework class in the Xcode source editor, you will get to the class header file. In a way this is the Black Box Principle in practice; you get to see what the class exposes as it’s interface, but not how it does it’s grunt work. That is reserved for the implementation file. The one exeption to this is that a header file also defines any private properties of a class, so these are also exposed.
An Obj-C header file defines what instance variables, properties og functions a class has, which class it inherits from and the interfaces it adheres to. It is declared with the @interface keyword:
| 1 | @interface MyClass : MySuperClass |
@interface keyword, followed by class name, a colon and the super class name. For the most bare-bones objects, you extend NSObject.
You see, this might be the reason you would think a header file is like an AS3 interface, and it sort of is. But don’t put that one in the bank and move on just yet because you need to be aware that Objective-C has something called “protocols” which is semantically more similar to AS3 interfaces. We will deal with protocols in a later part, but for now here’s an illustration to help you map you understanding of these concepts to AS3:

In general you could say that the Objective-C header file is a combination of the AS3 class header and the AS3 interface, while the AS3 interface is a combination of the Objective-C header file and Objective-C protocols.
A complete abstract header file:
| 1 | #import "SomeClass.h" |
| 2 | @class SomeOtherClass; |
| 3 | @interface MyClass : MySuperClass { |
| 4 | DataType *myProtectedVariable; |
| 5 | } |
| 6 | |
| 7 | @property (strong, nonatomic) DataType *propertyName; |
| 8 | -(void)doSomething; |
| 9 | @end |
Now what’s all this? First of all, if you read part 1, you know that the #import keyword is just like imports in AS3. But here you see yet another kind of import:
@class imports
| 1 | @class DetailViewController; |
Quite simply, importing with @class does no more than tell the compiler that the class exists. This is called a forward declaration. The compiler will defer validation of the existance of the class to compile time. If your implementation uses properties or functions of this class, you will need to #import the class instead.
A way to remember which is which is to compare #import to ActionScript’s #include directive since that also uses the # sign and it inlines the code of the class. Another way is to notice that the @class keyword does not use a file extension (MyClass, not MyClass.h). This gives you an indication that @class is a general declaration, not an import.
Instance variables (ivars)
These are defined in what actually looks like a constructor, but this being a header file it’s not. (Actually, Obj-C doesn’t have explicit constructors, more on this in a later part).
| 1 | protected var myProtectedVariable:DataType; |
| 1 | @interface MyClass : MySuperClass { |
| 2 | DataType *myProtectedVariable; |
| 3 | } |
Defining variables like this makes them local to the instance of the class. By default these variables are protected, making them accessible to the class and any subclasses, but not from the outside. Ivars can also be specified as private or public, though this is rare and highly discouraged.
Properties
| 1 | // In an interface |
| 2 | function get propertyName():DataType |
| 3 | function set propertyName(value:DataType):void |
| 1 | @property (strong, nonatomic, readwrite) DataType *propertyName; |
From left to right: @property keyword, property modifiers in parethesis (see below), the data type, which by C convention is always listed before the property/variable name, and lastly the propertyName, with or without the asterisk (see below).
A property is an externally accessible variable. It is not a public property and in the context of the header file it is merely a declaration stating to other classes that this is a property you can access. You will have to provide getter/setter implementations in the .m file to support this declaration. Think of it as declaring getter and setter functions in an AS3 interface.
Pointers
| 1 | @property (strong, nonatomic, readwrite) DataType *propertyName; |
You might have noticed that asterisk preceeding the property name. This declares that this variable is a pointer. A pointer is a reference to the memory space that an object occupies. By storing pointers to objects you don’t maintain copies of them throughout your application but rather references to the same instance.
Pointers do not have direct AS3 counterparts because AS3 is a higher level language that does not provide this level of memory management. In AS3 some datatypes are implicitly passed by reference/pointers (such as arrays) and others are passed by value, like strings. On a side note, in Objective-C strings are instances of NSString and as such they too are passed as pointers.
In Objective-C that asterisk is present whenever a pointer is declared, be it in a property, private variable or local variable. It is important to remember that function arguments are also declarations of local variables, hence you must include the asterisk there as well if the value you expect should be a pointer/reference.
Property modifiers
To assist in memory management, thread safety and access control, Objective-C has a number of possible modifiers you can specify in the property declaration. These are the words in parethesis following the @property keyword as seen in the example above:
| 1 | @property (strong, nonatomic, readwrite) DataType *propertyName; |
Regarding the memory modifiers, you should be aware that iOS 5 introduced automatic reference counting (ARC) which simplifies memory management and thus changed the list of possible modifiers. iOS Memory management and thread safety is beyond the scope of this post series. See Apple iOS Center documentation for these.
The access modifiers do have AS3 equivalents: Specifying readwrite is the same as implementing a getter and a setter, while readonly is the same as only implementing a getter.
Property modifiers can appear in any order. The default property modifiers are weak, atomic, readwrite and need not be specified.
Functions
In the example above you saw a function signature at its simplest:
| 1 | public function doSomething():void |
These are more appropriately called methods because they are exposed and as such part of the class interface. The minus sign denotes an instance method, as opposed to a static method which is prefixed by a + sign:
| 1 | public static function doSomething():void |
With arguments, functions look like this:
| 1 | public function doSomething(soleArgument:DataType):void |
| 1 | -(void)doSomething: (DataType *) soleArgument; |
That’s all nice and well. The asterisk pointer indicater is placed inside the data type declaration parenthesis. If this was a primitive value, like an integer, the asterisk would be omitted. Look at what happens when you include multiple arguments. In this case I wrote the example a little less abstract to better convey why this is:
| 1 | public function doSomethingWith(myArgument:DataType, numRepetitions:int):void |
| 1 | -(void) doSomethingWithMyObject: (DataType *) theObject forThisManyTimes:(int) numRepetitions; |
Whoah! That’s definitely not like ActionScript. Essentially you should read that signature like “do something with my object for this many times”. The function name and the first argument are together seen as the complete function name, whereas additional arguments add to it like a sentence. It sort of looks like you define the name of the second argument twice (and any additional arguments after that), but it’s actually not that weird. The name before the data type parethesis is the externally visible argument name (i.e. the name that pops up in the content assist), while the name after the parenthesis is the definition of the local variable exposed inside the function’s implementation.
What about return types? That’s easy:
| 1 | public function getSomething():DataType |
| 1 | -(DataType) getSomething; |
I’ll leave you with that for now