While I ponder what next to add to Nodality, I thought I'd take some time learning Swift and playing with Xcode 6. If you're not aware, Swift is Apple's new programming language for both iOS and OS X development. For an ActionScript old timer like me, it's a lot friendlier that Objective C and, unlike developing with AS3 and AIR, gives me immediate access to all the great stuff in iOS like SpriteKit, SceneKit and Metal.
So, my first throwaway project is a silly calculator containing two horizontal sliders, to set values, and a segmented button control (similar to a Flex ButtonBar) to select the operator. Changing either slider or the segmented button updates a bigger label with the result of the calculation.
To kick off, I used Xcode's story board to build the user interface with drag and drop. FlashBuilder no longer has a design view, so I'd forgotten how easy it is. XCode has nice Illustrator-esque alignment helpers too - so it's a breeze getting my basic UI to look reasonably nice:
XCode has already created me a ViewController class, so the next step is to create references to the UI controls in that. This is done with control-drag from the design view into the code view:
The little blue line indicates where Xcode will add its reference. First off, I need to create Outlets, in this example, I suppose the nearest Flex analogy is that an outlet is like a SkinPart. After releasing the mouse, I get this little callout that allows me to name the outlet:
Which creates a line of code like this:
@IBOutlet weak var sliderOne: UISlider!
Now that I have a reference to the UI controls, I can start writing code. In the ViewController, I want to set the minimum and maximum values of the sliders and set the default operator. I do this my overriding viewDidLoad() - which is invoked after the view loads, natch - and adding my own function, setUpUserInterface().
override func viewDidLoad()
{
super.viewDidLoad()
setUpUserInterface();
}
Every ActionScript developer will understand the code inside setUpUserInterface():
private func setUpUserInterface() -> Void
{
sliderOne.minimumValue = 0;
sliderOne.maximumValue = 100;
sliderTwo.minimumValue = 0;
sliderTwo.maximumValue = 100;
operatorButton.selectedSegmentIndex = 0;
}
The only differences are that the function's return type uses '->' rather than ':' (and since Swift infers type, it's totally unnecessary) and the function keyword is func.
With AS3, I'd now start to add event listeners to react to user gestures. These are handled by actions in Swift and created with the control-dragging blue line method I used for creating outlets. If I change the connection type to action, the callout looks a little different (I've opened the event combo to show the different handlers that can be created):
Now, we have an action method which, after I've added a few lines of code, looks like this:
@IBAction func sliderOneChange(sender: AnyObject)
{
labelOne.text = NSString(format: "%.2f", sliderOne.value);
updateTotal();
}
Finally, after adding
private func updateTotal() -> Void
{
var total:Float;
switch(operatorButton.selectedSegmentIndex)
{
case 0:
total = sliderOne.value + sliderTwo.value;
case 1:
total = sliderOne.value - sliderTwo.value;
case 2:
total = sliderOne.value * sliderTwo.value;
case 3:
total = sliderOne.value / sliderTwo.value;
default:
total = 0;
}
resultLabel.textColor = total < 0 ? UIColor.redColor() : UIColor.blackColor();
resultLabel.text = NSString(format: "%.2f", total);
}
Xcode 6 comes bundled with a Git client, so a moment later, my first ever experiment with Swift is safely stored at GitHub for everybody to enjoy.
I'll be blogging regularly while I learn Swift and I'm also a frequent tweeter - please join me on my journey at: @FlexMonkey.
Add a comment