{"id":164,"date":"2010-03-28T13:54:26","date_gmt":"2010-03-28T06:54:26","guid":{"rendered":"http:\/\/blog.bebensiganteng.com\/?p=164"},"modified":"2016-03-14T08:13:23","modified_gmt":"2016-03-14T08:13:23","slug":"actionscript3-to-cocoa-part-1","status":"publish","type":"post","link":"https:\/\/rahmat-hidayat.com\/?p=164","title":{"rendered":"AS3 to Cocoa : Part 1"},"content":{"rendered":"<p>I&#8217;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&#8217;ve found a massive gap separating between the two.<\/p>\n<p>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 <a href=\"http:\/\/www.bit-101.com\/blog\/?page_id=1868\" target=\"_blank\">Keith Peters<\/a> has written in his blog, but again as an autodidact programmer reading those tutorials was difficult, what is pointers? why is <em>NSObject<\/em>? but eventually with much persistence and an occasional mental breakdown, I&#8217;ve finally had a grasp on the language.<\/p>\n<p>If you want to follow this tutorial, first ensure you&#8217;ve downloaded all of the necessities and also I&#8217;m assuming have a decent understanding on AS3 and you&#8217;ve had a bit of a tinker with the Xcode otherwise, click <a href=\"http:\/\/www.youtube.com\/results?search_query=xcode+tutorial&amp;search_type=&amp;aq=f\">here<\/a>.<\/p>\n<p>What we are about to do is obviously nothing complex, just something to familiarize ourselves with the Objective-C concept.<\/p>\n<p>First open your Xcode and select New Project which will open the New Project panel.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/lh3.ggpht.com\/_sUijEfb5GxY\/S65CSOMwScI\/AAAAAAAAATA\/Ad5HPmqUC2M\/s400\/Step1.gif\" alt=\"New Project Panel - AS3 to Cocoa Touch part 1\" \/><br \/>\nOn the New Project Panel under Mac OS X tab select Command Line Utility and select the Foundation Tool.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/lh5.ggpht.com\/_sUijEfb5GxY\/S65HZt4gWKI\/AAAAAAAAATE\/XMoK0EEkBAk\/s400\/Step2.jpg\" alt=\"Author Environment - AS3 to Cocoa Touch part 1\" \/><br \/>\nIf everything went well you should now be at the authoring environment just as illustrated above and open the message file if you haven&#8217;t done so, which is denoted by the .m extension.<\/p>\n<p>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.<\/p>\n<p>So let&#8217;s create a simple interface for MyPet class.<\/p>\n<pre class=\"brush: objc; title: ; notranslate\" title=\"\">\r\n@interface MyPet : NSObject  {\r\n\tNSString *petName;\r\n}\r\n\r\n- (void) makeSound;\r\n- (void) setName:(NSString*) n;\r\n- (NSString *) getName;\r\n\r\n@end\r\n<\/pre>\n<p>If this is the first time you&#8217;ve seen Objective-C it may appear alien at the moment, but let see the AS3 equivalent of MyPet class interface.<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage com.farm {\r\n\r\n\tpublic interface MyPet {\r\n\r\n\t\tfunction makeSound():void;\r\n\t\tfunction setName(n:String):void;\r\n\t\tfunction getName():String;\r\n\r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>As we can see there are some slight differences, a few elements to note are the <a href=\"http:\/\/developer.apple.com\/mac\/library\/documentation\/Cocoa\/Reference\/Foundation\/Classes\/NSObject_Class\/Reference\/Reference.html\">NSObject<\/a>, which is in a nutshell is a base interface, the definition of <em>petName<\/em> as the class variable and the absent of a package.<\/p>\n<p>Now let&#8217;s proceed with the implementation.<\/p>\n<pre class=\"brush: objc; title: ; notranslate\" title=\"\">\r\n@implementation MyPet\r\n\r\n- (void) makeSound\r\n{\r\n\tNSLog(@&quot;Tweets&quot;);\r\n}\r\n\r\n- (void) setName:(NSString *) n;\r\n{\r\n\tpetName = n;\r\n}\r\n\r\n- (NSString *) getName\r\n{\r\n\treturn petName;\r\n}\r\n\r\n@end\r\n<\/pre>\n<p>and the AS3 equivalent.<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage com.animal\r\n{\r\n\timport com.farm.*;\r\n\r\n\tpublic class Bird implements MyPet\r\n\t{\r\n\r\n\t\tprivate var _petName:String;\r\n\r\n\t\tpublic function makeSound():void\r\n\t\t{\r\n\t\t\ttrace(&quot;Tweets&quot;);\r\n\t\t}\r\n\r\n\t\tpublic function setName(n:String):void\r\n\t\t{\r\n\t\t\t_petName = n;\r\n\t\t}\r\n\r\n\t\tpublic function getName():String\r\n\t\t{\r\n\t\t\treturn _petName;\r\n\t\t}\t\r\n\t}\r\n}\r\n<\/pre>\n<p>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.<\/p>\n<p>Finally the instantiation.<\/p>\n<pre class=\"brush: objc; title: ; notranslate\" title=\"\">\r\nint main (int argc, const char * argv[]) {\r\n    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];\r\n\r\n\tMyPet *bird = [MyPet new];\r\n\r\n\t[bird setName:@&quot;Polly&quot;];\r\n\t[bird makeSound];\r\n\r\n\tNSString *birdName = [bird getName];\r\n\tNSLog(@&quot;%@&quot;,birdName);\r\n\r\n\t[bird release];\r\n\r\n    [pool drain];\r\n    return 0;\r\n}\r\n<\/pre>\n<p>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.<\/p>\n<p>Other elements are the @&#8221;Polly&#8221; which is just the way you pass string inside Objective-C and NSLog(@&#8221;%@&#8221;,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.<\/p>\n<p>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 &#8220;Tweets&#8221;.<\/p>\n<p>So that&#8217;s the end of part 1 there are a lot of things that can be explained here, by I&#8217;ll leave it as it so it&#8217;s easier to understand.<\/p>\n<p>And the AS3 equivalent.<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\npackage \r\n{\r\n\timport com.animal.*;\r\n\r\n\timport flash.display.MovieClip;\r\n\r\n\tpublic class Main extends MovieClip \r\n\t{\r\n\t\tpublic function Main()\r\n\t\t{\r\n\t\t\tvar bird:Bird = new Bird();\r\n\r\n\t\t\tbird.setName = &quot;Polly&quot;;\r\n\t\t\tbird.makeSound();\r\n\t\t\t\r\n\t\t\tvar birdName:String = bird.getName();\r\n\r\n\t\t\ttrace(birdName);\r\n\t\t}\r\n\t}\r\n\t\r\n}&lt;\/code&gt;\r\nand here's the entire code.\r\n\r\n&lt;code lang=&quot;objc&quot;&gt;\r\n@interface MyPet : NSObject  {\r\n\tNSString *petName;\r\n}\r\n\r\n- (void) makeSound;\r\n- (void) setName:(NSString*) n;\r\n- (NSString *) getName;\r\n\r\n@end\r\n\r\n@implementation MyPet\r\n\r\n- (void) makeSound\r\n{\r\n\tNSLog(@&quot;Tweets&quot;);\r\n}\r\n\r\n- (void) setName:(NSString *) n;\r\n{\r\n\tpetName = n;\r\n}\r\n\r\n- (NSString *) getName\r\n{\r\n\treturn petName;\r\n}\r\n\r\n@end\r\n\r\nint main (int argc, const char * argv[]) {\r\n    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];\r\n\r\n\tMyPet *bird = [MyPet new];\r\n\r\n\t[bird setName:@&quot;Polly&quot;];\r\n\t[bird makeSound];\r\n\r\n\tNSString *birdName = [bird getName];\r\n\tNSLog(@&quot;%@&quot;,birdName);\r\n\r\n\t[bird release];\r\n\r\n    [pool drain];\r\n    return 0;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,12,25,34,43,54],"tags":[64,75,122,144],"_links":{"self":[{"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/posts\/164"}],"collection":[{"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=164"}],"version-history":[{"count":2,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":1629,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=\/wp\/v2\/posts\/164\/revisions\/1629"}],"wp:attachment":[{"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahmat-hidayat.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}