AS3 to Cocoa : Part 1

I’ve been learning an awful lot of Cocoa lately, and to be honest at first I thought the syntax would be somewhat similar to what AS3 is, but apparently I’ve found a massive gap separating between the two.

There are a lot of tutorials and forums out there that can guide you into the right direction and some of them are written by flash aficionados and one of the good one is what the revered Keith Peters has written in his blog, but again as an autodidact programmer reading those tutorials was difficult, what is pointers? why is NSObject? but eventually with much persistence and an occasional mental breakdown, I’ve finally had a grasp on the language.

If you want to follow this tutorial, first ensure you’ve downloaded all of the necessities and also I’m assuming have a decent understanding on AS3 and you’ve had a bit of a tinker with the Xcode otherwise, click here.

What we are about to do is obviously nothing complex, just something to familiarize ourselves with the Objective-C concept.

First open your Xcode and select New Project which will open the New Project panel.

New Project Panel - AS3 to Cocoa Touch part 1
On the New Project Panel under Mac OS X tab select Command Line Utility and select the Foundation Tool.

Author Environment - AS3 to Cocoa Touch part 1
If everything went well you should now be at the authoring environment just as illustrated above and open the message file if you haven’t done so, which is denoted by the .m extension.

Objective-C is all about Object Oriented Programming (OOP) and its quite strict as oppose to AS3, creating a class has to be preceded by defining the method(s) and variable(s) on the interface first.

So let’s create a simple interface for MyPet class.

@interface MyPet : NSObject  {
	NSString *petName;
}

- (void) makeSound;
- (void) setName:(NSString*) n;
- (NSString *) getName;

@end

If this is the first time you’ve seen Objective-C it may appear alien at the moment, but let see the AS3 equivalent of MyPet class interface.

package com.farm {

	public interface MyPet {

		function makeSound():void;
		function setName(n:String):void;
		function getName():String;

	}

}

As we can see there are some slight differences, a few elements to note are the NSObject, which is in a nutshell is a base interface, the definition of petName as the class variable and the absent of a package.

Now let’s proceed with the implementation.

@implementation MyPet

- (void) makeSound
{
	NSLog(@"Tweets");
}

- (void) setName:(NSString *) n;
{
	petName = n;
}

- (NSString *) getName
{
	return petName;
}

@end

and the AS3 equivalent.

package com.animal
{
	import com.farm.*;

	public class Bird implements MyPet
	{

		private var _petName:String;

		public function makeSound():void
		{
			trace("Tweets");
		}

		public function setName(n:String):void
		{
			_petName = n;
		}

		public function getName():String
		{
			return _petName;
		}	
	}
}

I think up to this point everything would made sense now and of course Objective-C has a few execution directives we have to follow, such as the @implementation and @end.

Finally the instantiation.

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	MyPet *bird = [MyPet new];

	[bird setName:@"Polly"];
	[bird makeSound];

	NSString *birdName = [bird getName];
	NSLog(@"%@",birdName);

	[bird release];

    [pool drain];
    return 0;
}

In here you must be wondering what the main method is doing there. Think about the initial execution inside a document class in AS3, every Objective-C will be initiated with the main method which the compiler will call, passing a few parameters along the way to check the code.

Other elements are the @”Polly” which is just the way you pass string inside Objective-C and NSLog(@”%@”,birdName); which is just an equivalent of trace in AS3 and the most important thing is the [bird release]; method which basically is how the compiler releases the bird instant from the memory, garbage collection is paramount if you trying to build an iPhone application since it has limited resources.

Now run the code by pressing command-return and open the console window to see the output by pressing command-shift-R, if you are correct you will see the bird name and the “Tweets”.

So that’s the end of part 1 there are a lot of things that can be explained here, by I’ll leave it as it so it’s easier to understand.

And the AS3 equivalent.

package 
{
	import com.animal.*;

	import flash.display.MovieClip;

	public class Main extends MovieClip 
	{
		public function Main()
		{
			var bird:Bird = new Bird();

			bird.setName = "Polly";
			bird.makeSound();
			
			var birdName:String = bird.getName();

			trace(birdName);
		}
	}
	
}</code>
and here's the entire code.

<code lang="objc">
@interface MyPet : NSObject  {
	NSString *petName;
}

- (void) makeSound;
- (void) setName:(NSString*) n;
- (NSString *) getName;

@end

@implementation MyPet

- (void) makeSound
{
	NSLog(@"Tweets");
}

- (void) setName:(NSString *) n;
{
	petName = n;
}

- (NSString *) getName
{
	return petName;
}

@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	MyPet *bird = [MyPet new];

	[bird setName:@"Polly"];
	[bird makeSound];

	NSString *birdName = [bird getName];
	NSLog(@"%@",birdName);

	[bird release];

    [pool drain];
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *