{"id":489,"date":"2011-11-19T22:49:51","date_gmt":"2011-11-19T21:49:51","guid":{"rendered":"http:\/\/www.aymericlamboley.fr\/blog\/?p=489"},"modified":"2014-11-01T14:58:52","modified_gmt":"2014-11-01T13:58:52","slug":"from-as3-to-objective-c","status":"publish","type":"post","link":"http:\/\/www.aymericlamboley.fr\/blog\/from-as3-to-objective-c\/","title":{"rendered":"From AS3 to Objective-C"},"content":{"rendered":"<p>Some days after my <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/my-thoughts-on-flash-and-its-recent-events\/\" target=\"_blank\">article<\/a> about what is happening to Flash developers, I have started to learn Objective-C and I already like it!<br \/>\nIn this post, I will try to compare ActionScript 3 to Objecitive-C. I&#8217;m really new to Objective-C so if you find any errors in what I say, please add a comment to correct me.<\/p>\n<p>According to Wikipedia : <em><a href=\"http:\/\/en.wikipedia.org\/wiki\/Objective-C\" target=\"_blank\">Objective-C<\/a> is a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Reflection_%28computer_science%29\" target=\"_blank\">reflective<\/a>, object-oriented programming language that adds <a href=\"http:\/\/en.wikipedia.org\/wiki\/Smalltalk\" target=\"_blank\">Smalltalk<\/a>-style messaging to the C programming language. Today, it is used primarily on Apple&#8217;s Mac OS X and iOS. Objective-C is the primary language used for Apple&#8217;s <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cocoa_%28API%29\" target=\"_blank\">Cocoa<\/a> API<\/em>.<\/p>\n<p><!--more--><\/p>\n<p>Objective-C syntax looks like this :<\/p>\n<pre lang=\"objc\">- (id) initWithFrame:(CGRect)frame withText:(NSString *) text {\r\n\r\n    self = [super initWithFrame:frame];\r\n    if (self) {\r\n        myText = [[NSString alloc] initWithString:text];\r\n    }\r\n    return self;\r\n}<\/pre>\n<p>Dammit! What is that!? This funny syntaxe comes from Smalltalk programming language, and I find it has been simplified. Hmm, don&#8217;t run away! Objective-C is a beautiful programming language, and finally it&#8217;s syntax is easily readable.<br \/>\nHowever Objective-C has strong concepts, I recommend to learn a bit on C or Java (heir of Smalltalk too) before starting Objective-C. Personally, I&#8217;m not very familiar with these programming languages, but I learned them and it was easier to learn Objective-C thanks to their knowledge.<\/p>\n<p>Ok, so now let&#8217;s start!<\/p>\n<p><strong>Memory :<\/strong> First of all, in Objective-C (with the last Xcode version) there is a Garbage Collector that you can activate or not! I recommend to do not activate it at first, you should know how manage memory. There are lots of keywords which reference to memory manager : <em>alloc, dealloc, release, retain<\/em>, and there are also pointers. Memory handling in Objective-C is really unique : it uses a counter to determine the number of instances of an object and when released. The object&#8217;s counter is called the <strong>retain count<\/strong>. Each object have one. For example if an object is used 3 times, its retain count is 3.<br \/>\n<em>alloc<\/em> add 1 to the retain count and <em>release<\/em> remove 1 to the retain count. If the retain count is equal to 0, <em>dealloc<\/em> method is automatically called and it killed the object. Don&#8217;t call the dealloc method yourself!<\/p>\n<p><strong>Keyword differences<\/strong> in AS3 some primitives keyword are :<\/p>\n<pre>true; false; null; this;<\/pre>\n<p>And in Objective-C :<\/p>\n<pre>YES; NO; nil; self;<\/pre>\n<p><strong>Variables :<\/strong> Objective-C is based on C, variable declarations are identical.<\/p>\n<pre lang=\"actionscript3\">\/\/AS3 :\r\nvar myBoolean:Boolean = true;\r\nvar myInteger:int = 4;<\/pre>\n<pre lang=\"objc\">\/\/Objective-C :\r\nBOOL myBoolean = YES;\r\nint myInteger = 4;<\/pre>\n<p>In Objective-C there are no keywords like <em>var<\/em> and <em>const<\/em>, the type is specified before the variable.<\/p>\n<p><strong>Object declarations :<\/strong><\/p>\n<pre lang=\"actionscript3\">\/\/AS3\r\nvar myObject:Object = new Object();<\/pre>\n<pre lang=\"objc\">\/\/Objective-C\r\nNSObject *myObject = [[NSObject alloc] init];<\/pre>\n<p>In Objective-C most of the objects use NS prefix, it comes from NeXtStep. Objective-C was developed by NeXt (company that Steve Jobs founded after Apple).<br \/>\nYou may have noticed the &#8220;*&#8221; character, yes there are pointers in Objective-C. Pointers are a special data type that points to a location in memory. When a variable is typed to a pointer, accessing that variable will redirect you to the value stored at the pointer\u2019s memory address. <a href=\"http:\/\/en.wikipedia.org\/wiki\/Pointer_%28computing%29\" target=\"_blank\">More informations on Pointers<\/a>. That&#8217;s mean myObject is a pointer that points to a NSObject instance somewhere in memory.<br \/>\nThe keyword &#8220;alloc&#8221; means that you allocate memory for the object. Then the &#8220;init&#8221; is the constructor, that&#8217;s equivalent to &#8220;()&#8221;;<\/p>\n<p><strong>Methods and constructors : <\/strong><\/p>\n<pre lang=\"actionscript3\">\/\/AS3\r\nnamespace function outputText():void {\r\n    trace(\"Hello World\");\r\n}<\/pre>\n<pre lang=\"objc\">\/\/Objective-C\r\n- (void) outputText {\r\n   NSLog(@\"Hello World\");\r\n}<\/pre>\n<p>Hey, it wasn&#8217;t to hard! &#8220;namespace&#8221; keyword in AS3 syntax can be replaced by public, private or protected. <strong>In Objective-C all object&#8217;s methods are public!<\/strong> All object&#8217;s methods start with a &#8220;-&#8220;, if we want to use a Class&#8217; function (so a static function) we use a &#8220;+&#8221;.<br \/>\nIf you want to use parameters :<\/p>\n<pre lang=\"actionscript3\">public function paramText(newText:String):void {\r\n   var myText:String = newText;\r\n}<\/pre>\n<pre lang=\"objc\">- (void) paramText:(NSSTring *)newText {\r\n   NSString *myText = [[NSString alloc] initWithString:newText];\r\n}<\/pre>\n<p>In Objective-C there isn&#8217;t default value in function&#8217;s parameters : no null, no &#8220;&#8221;&#8230; so if your function signature is :<\/p>\n<pre lang=\"actionscript3\">public function paramText(newText:String = \"\"):void<\/pre>\n<p>You have to write two functions in Objective-C and call the good one :<\/p>\n<pre lang=\"objc\">- (void) paramText\r\n- (void) paramText:(NSString *)newText<\/pre>\n<p>There is no name conflicted, because it isn&#8217;t the same signature! One is &#8220;paramText&#8221;, and the other one&#8221;paramText:&#8221;. It isn&#8217;t obvious at first&#8230; We calls this function :<\/p>\n<pre lang=\"actionscript3\">paramText();\r\nparamText(newText);<\/pre>\n<pre lang=\"objc\">[self paramText];\r\n[self paramText:newText];<\/pre>\n<p>A function with two params :<\/p>\n<pre lang=\"actionscript3\">paramText(myText, 4);\r\npublic function paramText(myText:String, myNumber:uint);<\/pre>\n<pre lang=\"objc\">[self paramText:newText withNumber:4];\r\n- (void) paramText:(NSSTring *)newText withNumber:(uint)myNumber;<\/pre>\n<p>If signature name is decently named, it is really friendly to read the function!<\/p>\n<p><strong>Constructors<\/strong> : like for functions, you can have several constructors and call the good one.<\/p>\n<pre lang=\"objc\">NSMutableArray *tab = [[NSMutableArray alloc] init];\r\n\/\/ OR :\r\nNSMutableArray *tab = [[NSMutableArray alloc] initWithCapacity:4];<\/pre>\n<p>Note that in Objective-C, NSArray like NSString&#8230; are static. That means after their initialization, their value can&#8217;t change! That&#8217;s why some of them have a mutable class : NSMutableArray, NSMutableString&#8230; For example : NSMutableString extends NSString, and add a new method : &#8220;appendString:&#8221;.<\/p>\n<p><strong>Classes :<\/strong> Objective-C extends C (all C code is compatible with Objective-C), so it has the same structure, two files : header (.h) and implementation (.m).<\/p>\n<p>A header is called an interface in Objective-C, it defines inheritance, properties and method signature. It looks like that :<\/p>\n<pre lang=\"objc\">#import \r\n\r\n@interface MyObject : NSObject {\r\n\r\n    NSString *text;\r\n}\r\n\r\n@property (nonatomic, retain) NSString *text;\r\n\r\n- (void) paramText;\r\n\r\n@end<\/pre>\n<p>MyObject extends NSObject. I&#8217;ve defined one propertie : text. By default, it is protected. You can change this by adding (@public, @protected, @private keywords) :<\/p>\n<pre lang=\"objc\">@interface MyObject : NSObject {\r\n    @public\r\n      NSString *text;\r\n\r\n    @private\r\n      NSString *text2;\r\n}\r\n@end<\/pre>\n<p>Then the @property keyword generates getter\/setter, you must precize them like I did if it is an object. But you change its options. In my example : when programm compiles it add automaticaly :<\/p>\n<pre lang=\"objc\">- (NSString *) text {\r\n    return text;\r\n}\r\n\r\n- (void) setUserName:(NSString *)text_ {\r\n    [text_ retain];\r\n    ;\r\n    text = text_;\r\n}<\/pre>\n<p>If you don&#8217;t precize nonatomic, it&#8217;s atomic. Quickly : atomic is use for multi-threading it gives more security on variable accessor, whereas nonatomic doesn&#8217;t; nonatomic is faster. Better explanation <a href=\"http:\/\/stackoverflow.com\/questions\/588866\/atomic-vs-nonatomic-properties\" target=\"_blank\">here<\/a> and <a href=\"http:\/\/stackoverflow.com\/questions\/821692\/what-does-the-property-nonatomic-mean\" target=\"_blank\">there<\/a>.<br \/>\nNote, even if you don&#8217;t define method in your header file you can create method in your implementation file. They will be hidden from outside (no autocompletion) like if they were private, but they are not! It can be useful if have to many functions and just would like to emphasize some of them.<\/p>\n<p>Moreover adding <em>@property<\/em> allow object properties to be use like in AS3 with a &#8220;.&#8221; :<\/p>\n<pre lang=\"actionscript3\">myView = new MyView(rect, myObject.myText);<\/pre>\n<pre lang=\"objc\"> myView = [[MyView alloc] initWithFrame:rect withText:myObject.myText];<\/pre>\n<p>A simpler exemple of using <em>@property<\/em> :<\/p>\n<pre lang=\"objc\">@property BOOL myBoolean;<\/pre>\n<p>Now the implementation :<\/p>\n<pre lang=\"objc\">#import \"MyObject.h\"\r\n\r\n@implementation MyObject\r\n\r\n@synthesize text;\r\n\r\n- (id) init {\r\n\r\n    self = [super init];\r\n\r\n    if (self) {\r\n\r\n        text = [[NSString alloc] initWithString:@\"my text\"];\r\n    }\r\n\r\n    return self;\r\n}\r\n\r\n- (void) paramText {\r\n    text = @\"my new text\";\r\n}\r\n\r\n- (void) dealloc {\r\n\r\n    ;\r\n\r\n    [super dealloc];\r\n}\r\n\r\n@end<\/pre>\n<p>The @synthesize refers to the @property. You must use to enable getter\/setter. It is really useful :<\/p>\n<pre lang=\"objc\">@synthesize property1, property2, property3;<\/pre>\n<p>Like in AS3 we call the parent method thanks to <em>super<\/em> keyword.<br \/>\nIn the init function, we return an <em>id<\/em> type. Objective-C is a dynamical language, you can use an object even if you don&#8217;t know its type thanks to the keyword <em>id<\/em> which is an object identifier. <em>id<\/em> is a pointer to an object.<br \/>\nKeyword dealloc is the descrutor, you must <em>release<\/em> your variables before calling [super dealloc];<\/p>\n<p>In Objective-C like AS3 there are no multiple inheritance, no operator overloading and no templates (there are in CPP). However we can use several interfaces like in AS3 :<\/p>\n<pre lang=\"actionscript3\">public class Bird extends Object implements IAnimals, IWings<\/pre>\n<pre lang=\"objc\">@interface Bird : NSObject <IAnimals, IWings><\/pre>\n<p><strong>MVC :<\/strong> Objective-C is based on the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Model%E2%80%93view%E2%80%93controller\" target=\"_blank\">MVC<\/a> pattern.<br \/>\nWhen a new project start in Xcode, 2 files are created : AppDelegate.h and AppDelegate.m it is like your Application&#8217;s Main. Then there are 3 useful classes : NSObject that you already know for your Model, UIView comes from UIKit (#import ) for your View, and finally UIViewController from UIKit too for your Controller. When a new file is created from one of this class, there are already useful methods like : <em>applicationWillResignActive, viewDidLoad, drawRect&#8230;<\/em>. This will be explain in an other tutorial.<\/p>\n<p>To conclude, Objective-C is an awesome programming language, very powerful (not just the language to use if you want to develop on Mac &amp; iOS). This first approach couldn&#8217;t explain all about it, there are still lots of things to learn&#8230; in a next tutorial \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some days after my article about what is happening to Flash developers, I have started to learn Objective-C and I already like it! In this post, I will try to compare ActionScript 3 to Objecitive-C. I&#8217;m really new to Objective-C so if you find any errors in what I say, please add a comment to &hellip; <a href=\"http:\/\/www.aymericlamboley.fr\/blog\/from-as3-to-objective-c\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">From AS3 to Objective-C<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[4,97,6],"tags":[15,73,72,34,99,102,101,100,74],"_links":{"self":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/489"}],"collection":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/comments?post=489"}],"version-history":[{"count":18,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/489\/revisions"}],"predecessor-version":[{"id":1287,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/posts\/489\/revisions\/1287"}],"wp:attachment":[{"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/media?parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/categories?post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.aymericlamboley.fr\/blog\/wp-json\/wp\/v2\/tags?post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}