<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>objective c &#8211; Software, Fitness, and Gaming &#8211; Jesse Warden</title>
	<atom:link href="https://jessewarden.com/tag/objective-c/feed" rel="self" type="application/rss+xml" />
	<link>https://jessewarden.com</link>
	<description>Software &#124; Fitness &#124; Gaming</description>
	<lastBuildDate>Tue, 08 Feb 2011 18:06:22 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://jessewarden.com/wp-content/uploads/2016/08/cropped-Lambda2-32x32.png</url>
	<title>objective c &#8211; Software, Fitness, and Gaming &#8211; Jesse Warden</title>
	<link>https://jessewarden.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Cocoa Chronicles #1: ActionScript vs. Objective C</title>
		<link>https://jessewarden.com/2010/04/cocoa-chronicles-1-actionscript-vs-objective-c.html</link>
					<comments>https://jessewarden.com/2010/04/cocoa-chronicles-1-actionscript-vs-objective-c.html#comments</comments>
		
		<dc:creator><![CDATA[JesterXL]]></dc:creator>
		<pubDate>Mon, 26 Apr 2010 16:59:08 +0000</pubDate>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective c]]></category>
		<guid isPermaLink="false">http://jessewarden.com/?p=2146</guid>

					<description><![CDATA[Skip Intro *** Update: Paul Taylor (@guyinthechair) has a more recent post on the same subject. *** *** NOTE: I&#8217;m a n00b at Cocoa and Objective C. Unlike Python, I don&#8217;t like learning it, either. If you perceive something below is inaccurate, please comment, and I&#8217;ll update. *** Preface I was having a lot of [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a href="http://jessewarden.com/2010/04/cocoa-chronicles-1-actionscript-vs-objective-c.html#intro">Skip Intro</a></p>
<p>*** <strong>Update</strong>: Paul Taylor (<a href="http://twitter.com/guyinthechair">@guyinthechair</a>) has a <a href="http://guyinthechair.com/2011/02/flash-to-objective-c-syntax-part-1/">more recent post</a> on the same subject. ***</p>
<p>*** NOTE: I&#8217;m a n00b at Cocoa and Objective C.  Unlike Python, I don&#8217;t like learning it, either.  If you perceive something below is inaccurate, please comment, and I&#8217;ll update. ***</p>
<p><strong>Preface</strong></p>
<p>I was having a lot of fun using the <a href="http://labs.adobe.com/technologies/flashcs5/appsfor_iphone/">iPhone packager</a>.Â Then <a href="http://daringfireball.net/2010/04/iphone_agreement_bans_flash_compiler">Apple changed their licensing</a> to prevent people like me from using ActionScript for iPhone/iPad, and instead being forced to use their toolsets, or those unofficially approved.  At first I was furious.  Then I tried to get a simple list to scroll in AS3.  Even using a fake mask, device fonts, and cacheAsBitmap for all items + container, it just wasn&#8217;t smooth, even with just 30 items, regardless of frame rate.  If you use an out of the box UITableView in Cocoa, it &#8220;just works&#8221;.</p>
<p><span id="more-2146"></span>I built my career on Flash Player because &#8220;it just works&#8221;.  It doesn&#8217;t &#8220;just work&#8221; on the iPhone, and now it&#8217;s effectively illegal to use.  That, and Adobe has apparently <a href="http://www.mikechambers.com/blog/2010/04/20/on-adobe-flash-cs5-and-iphone-applications/">abandoned ship</a> on the iPhone/iPad/iTouch portion, and instead are focusing on Android.</p>
<p>I was amazed at what I could do using Flash Lite 2 on my Nokia 6680 3 years ago.  AS3 was all the rage, and yet I took the time to build a <a href="http://code.google.com/p/shurikencomponents/">component framework</a> in AS2 for use on devices because I felt it was &#8220;good enough&#8221; to build experiences for consumers.  That market never materialized unless you targeted Japan (which I didn&#8217;t).</p>
<p>After finally getting my simple SWF to compile to the iPhone, I was estatic at how easy it was to design &amp; code in what I knew, it just worked, and looked like I expected it to.  Once I started moving things via <a href="http://www.greensock.com/tweenlite/">TweenLite</a>, though, it all went downhill.  I don&#8217;t have these problems in Flex.  If I use a List control in Flex, it just works; I don&#8217;t have to be an AS3 game developer to theorize on GPU acceleration possibilities to get the best list scrolling performance.  I don&#8217;t have these problems, at least from what I&#8217;m initially seeing in my early learnings, with Cocoa.</p>
<p>So, I&#8217;m learning Objective C so I can execute the ideas I have for iPhone and iPad.  Unlike PHP, Ruby on Rails, and Python &amp; Django, this has been a pretty miserable experience.  Python and Django are my choices for back-ends currently&#8230; but I don&#8217;t really have a &#8220;choice&#8221; for front ends on iPhone/iPad.</p>
<p>In an effort to reduce the pain for others as well as help myself retain this knowledge, I&#8217;m writing it down.</p>
<p>I understand it might seem strange since Android is a lot easier to develop for, and Flash Player 10.1 actually runs natively on the phone&#8230; and well, I might add.  For me, there are 3 reasons.  First, there is no market right now.  Yes, I understand we help create it, but I own an iPhone and iPad.  I love them, and so do others.  I have a gateway to others right now via the app store, and want to empower them to use my apps.  Secondly, if you&#8217;ve ever used an iPad, the additional screen real estate just fills you with ideas.  Third, everyone wants an app right now.  While the consumer data shows it&#8217;s hard to make money, it&#8217;s REALLY easy to make money for services; aka, a company hires you to build an app for them.  This is especially true with the OS4 announcement of Enterprises having more control over deploying their own applications.</p>
<p>Another good primer for me, Objective C for the ActionScript Developer, is Keith Peter&#8217;s five tutorials.  He doesn&#8217;t really cover the UIKit, just Objective C in general which is a great first start.</p>
<ol>
<li><a href="http://www.bit-101.com/blog/?p=1784">AS3 to iPhone Tutorial 1</a></li>
<li><a href="http://www.bit-101.com/blog/?p=1793">AS3 to iPhone Tutorial 2</a></li>
<li><a href="http://www.bit-101.com/blog/?p=1798">AS3 to iPhone Tutorial 3</a></li>
<li><a href="http://www.bit-101.com/blog/?p=1812">AS3 to iPhone Tutorial 4</a></li>
<li><a href="http://www.bit-101.com/blog/?p=1824">AS3 to iPhone Tutorial 5</a></li>
</ol>
<p><a name="intro"></a><strong>Introduction: Nomenclature</strong></p>
<p>There are a lot of new terms to learn.  I just want my code to run on the iPhone, yet I have to learn a bunch of new terminology to ensure I understand what it entails, as well as their &#8220;meaning&#8221;.  For example, MVC means something different to Objective C guys than it does to us.  Yes, it&#8217;s subtle, but when they say it, it&#8217;s assumed you mean &#8220;their&#8221; version.  It helps to know what the nomenclature of the Cocoa community is to learn, so first up, here&#8217;s some common terms.</p>
<p><strong><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html#//apple_ref/doc/uid/TP40002974-CH3-SW16">Cocoa</a></strong>: The API you code in for the Mac.  It&#8217;s like saying &#8220;Flex&#8221;; it&#8217;s all the things that fall under &#8220;Flex&#8221; like the compiler, the sdk, the ide, the runtime, etc.  However, it&#8217;s a lot more than that.  For example, there are Human Interface Guidelines that Apple puts out that Cocoa apps need to comply with (I don&#8217;t currently know how this is enforced, excluding iPhone/iPad).  For the sake of this article, all you care about is &#8220;Foundation&#8221; and &#8220;UIKit&#8221;, 2 libraries that make up a lot of what people know of as Cocoa for iPhone.  It&#8217;s also built on the MVC concepts we&#8217;re used to.  The main difference is Cocoa is huge; it is a layer atop an OS, whereas Flex is a layer atop the Flash Player.</p>
<p><strong><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163">Objective C</a></strong>: C like programming language you code in that borrows some cool things from Smalltalk, similar to how we code in ActionScript.  It&#8217;s from the 1980&#8217;s, and boy does it feel like it.  Anytime I start coding, I have Megadeth on in the background, my Pontiac Firebird Trans Am in the driveway does a turbo boost, and Fletch watches over me, commenting how it&#8217;s all ball bearings these days.</p>
<p><strong><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html#//apple_ref/doc/uid/TP40002974-CH3-SW20">Foundation</a></strong>: Provides all the low level stuff you need and shouldn&#8217;t have to write yourself.  This includes data types like NSNumber (vs using int/float yourself), sockets, etc.  Basically, if you can&#8217;t see it in a GUI, the class is here.  Everything is prefixed with &#8220;NS&#8221; for NextStep, one of the companies responsible for the early development.</p>
<p><strong><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html#//apple_ref/doc/uid/TP40002974-CH3-SW22">UIKit</a></strong>: All your GUI controls including support classes.  Things like UIButton, UIAcceleration, etc.  The UI prefix stands for User Interface (duh).  You&#8217;ll hear about references to Application Kit; that&#8217;s the desktop version.</p>
<p><strong>Core Data</strong>: Basically the model layer of your apps.  This is actually kind of cool, and what we should have in Flex (I know of 2 people who have tried to create an open source one, and then shortly thereafter abandoned the project&#8230; too bad too).  Right now, there are things like it in LiveCycle, I believe, but only Enterprises can afford that, so its&#8217; not for &#8220;everyday use&#8221;.  Basically, you create ValueObject&#8217;s, and they can be persisted in local XML or SQL databases automatically, vs. you having to manually write Factories, parsers, and versioning.  Pimp.</p>
<p><strong>Xcode</strong>: The IDE that you code Objective C in.  You can build &amp; debug your iPhone/iPad applications directly from here.</p>
<p><strong>Interface Builder</strong>: The IDE that you use to visually build your GUI apps.  You can actually create variable &amp; method definitions here, wire them up visually, and then have it code gen the classes for you.  It can also read your header files (imagine if every ActionScript class had an interface class associated with it that defined what it did).  As a n00b, I&#8217;ve found it really easy to confuse this program and make it explode.  Also, someone forgot to add tab order when they made it.</p>
<p><strong>Objective C</strong></p>
<p>Objective C is the programming language you write your applications in.  It is effectively an extension to ANSI C with Object Oriented syntax.  In Flash and Flex, you utilize ActionScript 3.  Objective C has a lot in common with C and C++, and some of the lamesauce as well, namely pointers.  Objective C also has multiple environs it can run on while ActionScript runs in just the Flash Player.</p>
<p>If you know ActionScript 3, you can read C, and thus Objective C.  Like Java, the type definitions are on the left instead of right, but other than that, they are extremely similar.  Like another article mentioned, if C didn&#8217;t have pointers, PHP and C would be nearly the exact same.  Since Objective C has a lot in common with C, they too are similar.</p>
<p>If you&#8217;re coding Objective C for the iPhone/iPad, you don&#8217;t get Garbage Collection, and have to manually get rid of your variables/objects.  Once you get used to the syntax, it&#8217;s almost the exact same as cleaning up your mess in ActionScript.  In ActionScript, you set your Object references to null; in Objective C it&#8217;s called nil.  More on that insanity later.</p>
<p>You can read more here about <a href="http://developer.apple.com/mac/library/documentation/cocoa/conceptual/ObjectiveC/Introduction/introObjectiveC.html">here</a>.</p>
<p>Yes, you code in Objective C 2, not 1.  1 is even more lame than 2 (thankfully?).</p>
<p><strong>Objective C Syntax: Methods</strong></p>
<p>Objective C has a lot of the same things that ActionScript 3 has.  They both use the same bracket syntax, and have the same cuddled vs. not wars.  They both end lines with semi-colons.  There isn&#8217;t as much dot syntax.  They both have methods.  Here&#8217;s how you invoke one in ActionScript:</p>
<pre lang="actionscript3">mySound.setVolume();</pre>
<p>Here&#8217;s how you invoke it in Objective C:</p>
<pre lang="objc">[mySound setVolume];</pre>
<p>Notice in Objective C, they use brackets to indicate the containment of a method call.  You can nest these as well, but they recommend you only go 2 deep since it becomes un-readable after that.</p>
<p>Here&#8217;s ActionScript storing a return value:</p>
<pre lang="actionscript3">volume = mySound.getVolume();</pre>
<p>Here&#8217;s Objective C doing the same:</p>
<pre lang="objc">volume = [mySound getVolume];</pre>
<p>Passing in parameters is a little different and takes some getting used to.  This, to me, is the same thing that threw me off in Ruby, and where Objective C totally ignores how C does it.  Here&#8217;s ActionScript:</p>
<pre lang="actionscript3">mySound.setVolume(value);</pre>
<p>And here&#8217;s Objective C:</p>
<pre lang="objc">[mySound setVolume:value];</pre>
<p>Ok, not so bad&#8230; except, the 1st parameter is always the only one WITHOUT a named parameter.  Observe the following ActionScript 3 function with 2 parameters:</p>
<pre lang="actionscript3">mySound.setLeftAndRightPan(left, right);</pre>
<p>&#8230;and Objective C:</p>
<pre lang="objc">[mySound setLeftAndRightPan:left, rightValue:right]</pre>
<p>Notice there is no &#8220;leftValue:left&#8221; here, just left.  The first parameter is always nameless, yet every one after it has one.  I guess they wanted more obvious parameters during function invocations&#8230; except forgot to build in one for the 1st parameter.  *face palm*</p>
<p>For example, here&#8217;s an ActionScript 3 function definition:</p>
<pre lang="actionscript3">private function setLeftAndRightPan(left:Number=0, right:Number=0):void</pre>
<p>Here&#8217;s the equivalent Objective C:</p>
<pre lang="objc">- (void)setLeftAndRightPan:(NSNumber*)left, right:(NSNumber*)rightValue</pre>
<p>Notice 4 important things.  First, the return type is in the beginning and wrapped in paranethesis (void in this case).  Second, notice that Objective C doesn&#8217;t allow for default values for function parameters like ActionScript.  Third, notice the type definitions for the function parameters are in the left side vs. the right (in this case NSNumber).  We&#8217;ll get to the asterisk later.  Fourth, notice the dash in the very beginning.  That signifies that this is an instance method on the class.  If you want ActionScript&#8217;s static, use + instead.</p>
<p><strong>Objective C Syntax: Variables</strong></p>
<p>Variables are slightly different because Objective C has pointers as well of built-in helper methods to make memory management easier.</p>
<p>Here&#8217;s a variable in ActionScript, first defined using a String literal (pimp syntax) and the old school way:</p>
<pre lang="actionscript3">private var name:String = "";
private var name:String = new String();</pre>
<p>And here&#8217;s one in Objective C:</p>
<pre lang="objc">NSString* name = [NSString string];</pre>
<p>There are a ton of caveats here.  Notice we&#8217;re calling the string method on NSString; it&#8217;s basically a factory method to give us a string; similar to going new String().  When I did &#8220;&#8221; above, it&#8217;s called a String literal; the same as going {} vs. new Object(), or [] vs. new Array().  In ActionScript, each of those constructors has nice features, the effect is the same; you get a variable of that type back.</p>
<p>Notice the asterisk in the Objective C example.  This means it&#8217;s a pointer to the String, not the actual String.  Also notice since we&#8217;re calling the string method on the NSString class, it&#8217;s autoreleasing (similar to the Doom spell in Final Fantasy; it&#8217;ll die soon; ie he&#8217;ll get ejected from the Pool soon, more on that later).  If you want the guy be like Number 1 and live forever instead of a Rico&#8217;s Roughneck, go:</p>
<pre lang="objc">NSString* name = [[NSString alloc] init]</pre>
<p>Just keep in mind you have to manually call release on him later whereas the former, you don&#8217;t.</p>
<p><strong>Objective C Syntax: Pointers &amp; Memory Management</strong></p>
<p>Variables are different because of a couple reasons.  First, since Objective C is based on C, it has all the pointer baggage.  We ActionScript Developers are spoiled because we don&#8217;t have do deal with this&#8230; or if we do, it requires little thought.</p>
<p>For example, in ActionScript, there are only 3 variable types that are by value, and thus don&#8217;t have pointers: Strings, Numbers, and Booleans.  Yes, I&#8217;m including int and uint as Numbers here.  Every other construct is an Object, and thus is passed by val.  If you wish for an Object based variable to be removed from memory eventually, you need to ensure no more references exist, or in the case of circular references, that no one else knows about the circular relationship (the Mark and Sweeper from the Garbage Collector will get those mofos).  For Numbers, Strings, and Booleans who aren&#8217;t local variables, you just remove their parent Object, and they&#8217;re toast.  For local variables, they go away at the end of the function (usually&#8230; depends on how aggressive the Garbage Collector is at that moment in time).</p>
<p>For Objects, you just remove all references, usually by setting them to null, or in the case of dynamic Objects like Dictionary or Object hash maps, using the delete keyword.</p>
<p>Objective C&#8217;s iPhone runtime, and ActionScript&#8217;s Flash Player both operate using reference counting.  This means, for Objects, an internal number is incremented for every person who &#8220;knows&#8221; about an Object.  When that person later set&#8217;s his reference to null, that reference count goes down.  When that reference count reaches 0, it&#8217;s eligible for Garbage Collection.  The Flash Player also has a Mark and Sweep part to take care of circular references in the case where 2 Objects know about each other, yet no one knows in your app knows about them, and thus their reference count will never reach zero.  If you&#8217;re running on the iPhone, you don&#8217;t have Garbage Collection, so need to call release yourself (which calls dealloc internally).  In ActionScript, you&#8217;d either null the reference out, or call your own destroy method, THEN null reference out.</p>
<p>Objective C has this built in as a method called dealloc.  You don&#8217;t call, it gets called for you when you (or someone else) calls release.  In ActionScript, people will either use a convention of public function destroy, or those fu@#(* IDestroy marker interfaces that people NEVER implement fully (did I mention that&#8217;s annoying?).</p>
<p>In ActionScript, for a complex class, it may look something like this:</p>
<pre lang="actionscript3">public function destroy():void
{
	if(timer)
	{
		timer.removeEventListener(TimerEvent.TIMER, onTick);
		timer.stop();
		timer = null;
	}

	if(icon)
		icon.bitmapData.dispose();
}</pre>
<p>In Objective C, this is built-in to NSObject, one of the main classes you often extend.  The Objective C version of the above would look like:</p>
<pre lang="objc">-(void)dealloc
{
   [timer release]
   [icon release]
   [super dealloc]
}</pre>
<p>As learn about how getter/setters are built into Objective C, you&#8217;ll see there is a better, and more common way to do the above (article I link to at the bottom describes this), but I need to get the point across here that you&#8217;re expected to clean up your own mess since Garbage Collection doesn&#8217;t exist, and everything can&#8217;t be an autorelease variable.</p>
<p><strong>No Local Variables, Hence Autorelease &amp; T3h Pool</strong></p>
<p>In ActionScript, we have local variables.  Basically, instead of storing it in memory, it&#8217;s stored in temporary memory that the stack is using (the stack of functions currently running).  When one stack item is done (your function is done), those variables are removed.  In ActionScript&#8217;s case, this isn&#8217;t immediate, but basically ensures that no one has references to those variables anymore.  Later, Garbage Collection will come along and eat&#8217; em.</p>
<p>Objective C doesn&#8217;t have local variables.  Yes, I know, I&#8217;m face palming as well.</p>
<p>*deep breath* There IS a common way to deal with it.  Your first thought may be to create a bunch of member variables; basically what game developers do for simple Object Pooling.  That&#8217;s ghetto, though.  You may think you could just create variables on the fly&#8230; the problem with that is you have to manually release them, and for return values, they&#8217;ll die before they get returned.</p>
<p>This is where autorelease comes in.  Remember before when I talked about the 2 ways to create variables?  Let&#8217;s revisit it, and show again how you kill things.</p>
<p>This creates a variable you must manually remove:</p>
<pre lang="objc">NSString* name = [[NSString alloc] init];
[name release];</pre>
<p>&#8230; and she&#8217;s dead.  However, here&#8217;s one that&#8217;ll be added to a pool, or rather &#8220;a Pool&#8221;:</p>
<pre lang="objc">NSString* name = [NSString string];</pre>
<p>This is the same as doing:</p>
<pre lang="objc">NSString* name = [[NSString alloc] init];
[name autorelease];</pre>
<p>You can then safely return these variables from functions, knowing that the caller will get it.  It&#8217;s also considered good practice to do, specifically for encapsulation.  There isn&#8217;t really a language construct to say &#8220;Hey dude, this guy is going to send you a variable it also has a pointer to, so please make sure you call his release first&#8221;.  Crap like that is insane; it&#8217;s just easier to get either copy&#8217;s of variables so you don&#8217;t inherit their pointer baggage, or use autorelease so you don&#8217;t piss off people getting variables from you, and causing leaks if they forget to kill you first.</p>
<p>Yes, all of that above is a shortened version of what you have to do because there are no local variables.  Yes, I&#8217;m face palming again.</p>
<p>If you&#8217;re curious about what this Pool is, imagine if a static class was created every time you ran some code.. and Garbage Collection ran immediately when you null&#8217;ed out your variables.  The first thing it did was keep a reference to any variable that has autorelease on it.  Then, 1 frame after the stack has been complete (all your code has run, and the next enter frame runs), it kills all the variables it has stored.  That&#8217;s the AS version of what an Objective C Pool is.</p>
<p><strong>Conclusions</strong></p>
<p>This sucks.  <a href="http://www.businessinsider.com/the-underlying-story-behind-adobes-failed-mobile-strategy-2010-4">I blame Adobe AND Macromedia for my suffering</a>.</p>
<p>BTW, if you&#8217;re looking for some pimp tutorials to learn this stuff, my compadre <a href="http://www.webappsolution.com/wordpress/">Brian</a> linked me to it, called &#8220;<a href="http://cocoadevcentral.com/">Cocoa Dev Central</a>&#8220;, specifically this <a href="http://cocoadevcentral.com/d/learn_objectivec/">article</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://jessewarden.com/2010/04/cocoa-chronicles-1-actionscript-vs-objective-c.html/feed</wfw:commentRss>
			<slash:comments>28</slash:comments>
		
		
			</item>
	</channel>
</rss>
