by Paul Taylor,
Feb. 8, 2011, under [
community
]
Face it: Apple’s done a great job with the iOS UI. Consider this an axiom for the rest of the post. I’m not going to try and convince you if you disagree, and I’m not going to rehash what boils down to a religious debate. Their HIG is clutch.
But programming for it is crazy! Have you seen this language? What is this shit?

Relax
This post is Objective-C syntax for the average Flash developer. It isn’t that scary. The colors are just different than you’re used to. And you’ll have to know or learn C, but don’t worry if you’re unfamiliar, I have faith you’ll pick it up quick enough. Because you’re creative and curious and you like learning new things dammit.
Syntax
So why haven’t you jumped into iOS? For me, it was the syntax. So now I’m going to compare the syntax of the languages from an AS3 dev’s point of view. In the end, I hope you won’t feel as intimidated as I did by this weirdly beautiful language.
Disclaimer
I’m still learning this stuff too. If I’m wrong on anything, please post it in the comments. I’m trying to disseminate useful information, things that took me a bit of studying to figure out, in the hopes that it won’t take you as long.
Primitives
There are a few keyword differences:
AS3 Objective-C
true YES
false NO
null nil
this self
Variables
Variable declaration and assignment:
// AS3
var myInteger:int = 1;
// Objective-C
int myInteger = 1;
You can see, this isn’t any different from C++, Java, C#, etc. The difference from AS3 is the lack of the required var or const keyword, and the type is specified before the variable.
Memory Allocation
Something we don’t typically think about in AS3: stack vs. heap allocation.
The Stack is a LIFO (last in, first out) pool of memory blocks that are allocated/deallocated very quickly. Each time you call a function, a block of memory is allocated for the function’s scope. When you return from the function, the block of memory is released back to the stack. This means stack-managed memory blocks are reused very frequently, therefore is mostly allocated from RAM or even the CPU’s cache, making it extremely fast.
The Heap is more complex, and allocates and deallocates memory based on usage patterns. I am not remotely familiar with how Objective-C allocates memory from The Heap, but in general, allocating/deallocating memory from the heap takes longer than from the stack.
Here’s a better explanation of the difference between the stack and the heap. This stuff is just generally good to know.
Values
Values are primitive types, allocated on the stack, and passed by value, or copied. That is, if you pass the variable as an argument to a function, the variable’s value is copied then passed in.
Structs
A struct (short for “structure”), is a type that stores multiple values. AS3 doesn’t have structs, but we typically mimic the behavior of structs with Value Objects (VOs). But when structs are passed, they are passed by value, not reference. When you pass a struct (such as CGRect in Objective-C), the struct is essentially copied and fed into the target method.
Pointers
Pointers are a special data type that points to a location in memory allocated from The Heap. When a variable is typed to a pointer, accessing that variable will redirect you to the value stored at the pointer’s memory address. Pointers typically point to object instances, partly because objects have state and it won’t do to pass objects by value.
More about pointers at Wikipedia here.
The Objective-C syntax to designate pointers is to add a star (*) between the type and the variable name:
UIButton *myButton = [[UIButton alloc] init];
This means var myButton is a pointer that points to a UIButton instance somewhere in memory.
Methods
Objective-C has no concept of access modifiers or namespaces. For the purpose of this writeup, I’ll only use public methods. For familiarity’s sake, I’m using the addChild method signature of the DisplayObjectContainer class in my examples. Also, this post isn’t meant to introduce any Objective-C paradigms, obviously Cocoa doesn’t do DisplayObjects and event listeners like Flash does.
Anatomy
In AS3, functions are declared in this pattern:
namespace function name(parameters):Type;
public function addChild(child:DisplayObject):DisplayObject;
In Objective-C, you have the exact same options, just moved around a bit:
-(Type)name: parameters;
-(DisplayObject *)addChild:(DisplayObject *)child;
So in comparison:
public function addChild(child:DisplayObject):DisplayObject;
-(DisplayObject *)addChild:(DisplayObject *)child;
Say you’re writing the addChild method signature of the DisplayObjectContainer class:
public function addChild(child:DisplayObject):DisplayObject;
-(DisplayObject *)add: (DisplayObject *)child
Multiple Parameters
In AS3, you want more parameters, you separate them by commas:
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
In Objective-C, you name them:
-(void)addEventListener:(NSString *)type listener:(SEL)listener useCapture:(BOOL)useCapture priority:(int)priority useWeakReference:(BOOL)useWeakReference
This is just to show 1-1 parity with AS3. Because the order and name of the parameters is what makes up the method name, you could also write this method something like this:
-(void)addEvent:(NSString *)type withListener:(SEL)callback usingCapture:(BOOL)phase withPriority:(int)priority andWeakReference:(BOOL)weakReference
This way, the method reads almost like a sentence: “add event type String, with listener SEL, using capture phase BOOL, with priority int, and weak reference BOOL.”
Static Functions
In AS3, you declare a static function with the static keyword:
public function instanceFunction():void;
public static function staticFunction():void;
In Objective-C, you put a + in front of the method declaration:
-(void)instanceFunction;
+(void)staticFunction;
Calling Methods
In AS3, if you’re calling a method, it’s straightforward like this:
mySprite.addChild(myChild);
Here’s the same thing in Objective-C:
[mySprite addChild:myChild];
And the two lines side-by-side:
mySprite.addChild(myChild);
[mySprite addChild:myChild];
Here’s calling our pretend addEventListener function from before:
//Calling our fictitious addEventListener method:
[dispatcher addEvent:@"mouseDown" withListener:^{/* do something */} usingCapture:no withPriority:0 andWeakReference:no];
Classes
In Objective-C, each class is composed of two files. One is called the header file (suffix .h), the other is the implementation file (suffix .m). This is a C convention, since C doesn’t have proper classes.
Constructors
ActionScript has one constructor, it’s a function named after the Class it’s constructing. There are language reasons for this, primarily because deep down, AS3 isn’t that different from JavaScript, and each class is really just one big closure. But that’s a topic for another time.
Objective-C has what are called “designated initializers.” A designated initializer is pretty much the same idea as a constructor. A designated initializer is just a function that returns self, the Object’s reference to itself (AS3′s equivalent of this). Since designated initializers don’t have to be named after the class, there can be many, each taking different initialization values. This is essentially constructor overloading.
So where in AS3, you’d write:
public function MyClass(required:Boolean, optional1:int = 0, optional2:int = 3)
You could write the following in Objective-C:
-(id)init; //no required options, can leave this out if you wish
-(id)init:(BOOL)required;
-(id)init:(BOOL):required withOption:(int)option1;
-(id)init:(BOOL):required withOption:(int)option1 andAnother:(int)option2;
-(id)init:(BOOL):required orJust:(int)option2;
By convention, initializers start with some variation of the word “init.”
Headers
A header file is loosely analogous to an Interface in ActionScript 3. The header file declares all the publicly available functions and getters/setters available on the object. The header also declares what our Class inherits from, and the interfaces our Class implements. For this reason, what ActionScript actually calls an Interface, Objective-C calls a Protocol.
In short, all the API is declared in header files.
Implementation
The second part of a Class is the implementation. The implementation is the actual method and property implementation of the header file for the Class.
Examples
ExampleClass.as:
package com.ptaylor.blog {
public class ExampleClass extends Object implements IMyInterface, IOtherInterface {
//Constructor
public function ExampleClass() {
//do stuff
}
}
}
ExampleClass.h:
#import <UIKit/UIKit.h>
@interface ExampleClass : NSObject <IMyInterface, IOtherInterface>
{
}
The @interface declaration tells Objective-C that this is a header file, followed by the header’s name, ExampleClass. The following colon (:) declares what type this Class will extend from. Protocol (AS3 Interfaces) implementation declaration happens after the superclass declaration, inside the < > block.
ExampleClass.m:
#import "ExampleClass.h"
@implementation ExampleClass
//constructor
-(id)init{
//do stuff
return self;
}
The @implementation declaration tells Objective-C what header this file is implementing.
That’s it. That’s all I’ve got. I’ve skimmed over and given you the fifty-thousand foot overview of the Objective-C continent. As always, post some shit in the comments if I’m wrong or you liked this or whatever.
Tags: ActionScript3, Objective-C, Syntax
This entry was posted
on Tuesday, February 8th, 2011 at 11:49 am and is filed under community.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.