Teknision is an industry leading software development firm specializing in applications that span the browser, desktop and devices.

We build user experiences designed for competitive advantage.
1280 Baseline Rd. Unit 201
Ottawa Ontario, Canada, K2C 0A9

Toll Free: 1.866.528.4010
Local: 613.728.4010

AS3 to Objective-C : Syntax

When I started to pick up Objective-C for iPhone development, the first thing that jumped out at me was how completely foreign the language looked compared to what I was used to seeing every day. Some of these things were pretty easy to reconcile, but others took a bit longer to wrap my head around.

Objective-C is a strict superset of C, which means that if you are familiar with C then picking up Objective-C should be no problem for you. If you have never looked at (or understood) the C language, then you’ve got some learning to do. I’ll cover some of the things you’ll need to get yourself started.

In ActionScript we’re used to using this as a reference to self, in Objective-C you use self.

For Boolean values (BOOL in Objective-C), we are used to using true/false. In Objective-C these are YES/NO.

When we declare objects in ActionScript3, they have a value of null, while NULL does exist in Objective-C, nil is more commonly used.

In ActionScript we’re used to declaring all variables like this:

[code lang="actionscript"]var myInt:int;[/code]

In Objective-C we declare variables with the type out in front. Also all objects are declared as pointers, so you put an asterisk in front of the variable name:

[code lang="objc"]int myInt;
NSString *myString;[/code]

In ActionScript we’re used to only declaring functions in interfaces, but in Objective-C all functions have to be declared. In ActionScript a function might be defined as:

[code lang="actionscript"]function myFunction():void;[/code]

In Objective-C the same function would be declared like:

[code lang="objc"]- (void)myFunction;[/code]

The “-” means that the function is an instance function. A class function (like static in ActionScript) would be declared with a “+”, so:

[code lang="objc"]+ (void)myClassFunction;[/code]

For functions that receive arguments, we’re used to declaring them as:

[code lang="actionscript"]function getPoint(x:int, y:int):Point;[/code]

In Objective-C, this can be declared two ways:

[code lang="objc"]- (CGPoint)getPoint: (int)x, (int)y;
//or
- (CGPoint)getPointWithX: (int)x andY: (int)y;[/code]

The benefit of the second type of declaration is that it is less ambiguous because it tells you exactly what arguments you need to pass.

In Objective-C interfaces have a different meaning than they do in ActionScript. An interface in Objective-C (MyClassName.h) is where class variables and functions are declared. In your interfaces you also make @property declarations for synthesized getters/setters (more on that in a bit). Your class (MyClassName.m) holds your constants, function definitions and @synthesize declarations (to match the @property declarations). The Objective-C equivalent of an interface in ActionScript is called a protocol. A protocol can be written in its own .h file (eg. MyClassNameDelegate.h) or in the same .h file as the interface (this would make sense is the protocol was specifically used by that class).

A protocol is declared like this:

[code lang="objc"]@protocol ProtocolName

- (void)myProtocolFunction;

@end[/code]

An Interface is declared like this:

[code lang="objc"]@interface ClassName : SuperClass {
//variable declarations go here, eg:
int myInt;
}

@property (readwrite) int myInt;

- (int)myInstanceFunction;
+ (void)myClassFunction;

@end [/code]

A class is declared like this:

[code lang="objc"]@implementation ClassName

@synthesize myInt;

- (int)myInstanceFunction {
//logic goes here
}

+ (void)myClassFunction {
//logic goes here
}

@end[/code]

The @property and @synthesize directives are used for getter/setter synthesis. Without the @property/@synthesize directives you would have to declare and define your getter and setter functions:

[code lang="objc"]- (int)myInt;
- (void)setMyInt: (int)inMyInt;[/code]

And these getters and setters would be accessed like so:

[code lang="objc"]int i = [obj myInt];
[obj setMyInt: 100];[/code]

By putting the lines…

[code lang="objc"]@property (readwrite) int myInt;
//and
@synthesize myInt;[/code]

…in my interface (.h) and class (.m), the compiler will synthesize those same functions so I don’t have to write those. By using the readwrite directive the compiler will generate both a getter and a setter. In addition to having these functions synthesized the @property/@synthesize directives also allow you to use Objective-C’s limited dot-notation syntax so I can now get and set the value like this:

[code lang="objc"]int i = obj.myInt;
obj.myInt = 100;[/code]

Hopefully, armed with this information, you can now look at Objective-C and understand what’s happening or even get started on your own projects.

  • Digg
  • del.icio.us
  • Google Bookmarks
  • NewsVine
  • Facebook
  • Twitter

1 Comment

  1. convoluted!

  2. Rahmat Hidayat - March 15, 2009 - 1:26 pm

RSS feed for comments on this post. TrackBack URI

Leave a Comment