AS3 to Objective-C : Data Types

Moving into iPhone development, has required most Flash/Flex developers to learn a new language – Objective-C. When you first get started with the language, it feels completely foreign, mostly because of the syntax is just so different. As you start to get comfortable with the language, you realize it requires a bit of a conceptual shift from the ActionScript mentality. For the basic ActionScript developer, here is a list of items/differences that might make the migration from into iPhone development just a bit easier for you.
Objective-C has in many ways retained its evolution history – the language itself still retains references to its predecssor – NeXTstep which you often see in the form of the prefix NS which often signifies that the variable type has added functionality. As Flash and Flex develoeprs, we’re used to seeing this type of namespacing artifcacts – the “mx” namespace being case in point although we’re beginning to see it phased out in Gumbo.
Variable Declaration and Type
The way that you declare variables and what type of variables you have differ between the two. In ActionScript, we strict type by declaring the variable and then its type – whereas in Objective-C you do just the opposite. For example, declaring a variable of type int:
In AS3:
[code lang="actionscript"]var i:int;[/code]
In Objective-C
[code lang="objc"]int i;[/code]
Debugging
Putting the code to the output window is as easy as putting something inside of a trace statement in AS3. Objective-C is similar, instead of trace, you use NSLog
Numeric DataTypes
In AS3, we can choose between Number, int, and uint. In Objective -C , you have many choices that replace just our typical understanding of Number – you have float, double or NSNumber. The float datatype is used for when you want to store variables that need to be correct up to six decimal places. The double datatype holds a double-precision floating point number so double the precision that float offers. Because of this, the double datatype takes up twice the amont of space as the float data type and you should use float whenever possible for performance and memory use.
Boolean
In AS3 we are used to booleans being true/false. In Objective-C, the values are YES and NO. Declaring a boolean type looks different as well:
AS3
[code lang="actionscript"]var loaded:Boolean = false[/code]
Objective-C
[code lang="objc"]BOOL loaded = NO;[/code]
Null
Variables that are defined, but do not have a value, are determined to be null in AS3. Null doesn’t exist in Objective-C – but is replaced by nil. The major difference is that in Objective-C you can call a method on nil without it erroring, like it will in AS3 – it will just return that nil value.
Strings
In AS3, we pretty much can interchange when we use single and double quotes for strings – with the exception of RegEx operations that require you use double quotes when doing a replacement. In Objective-C, there is a signficant difference between single and double quotes and their usage. Single quotes are for a single character assignment only:
[code lang="objc"]char firstCharacter ='A';[/code]
Double quotes are used for String assignments- but there are several things to note here as well. Just enclosing a string in double quotes creates the primitive C style char[] string – which is really just an array with some string specific functionality.
[code lang="objc"]char myName[5] = "Jason";[/code]
For more functionality, you would want to use the Objective-C NSString object type. To create or declare a string of this type you need to use the “@” – which is a special construct that will create an NSString from ASCII ( NSString is unicode). Its basically a shortcut method for instantiation in this use, and allocates memory for the object.
So the syntax looks like this:
[code lang="objc"]@"Jason"[/code]
or
[code lang="objc"]NSString *myString = @"Jason";[/code]



