Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A First Look at Swift and Xcode 6 - An App in Ten Minutes!

DZone's Guide to

A First Look at Swift and Xcode 6 - An App in Ten Minutes!

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.



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:

@IBOutletweakvar 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().
   
1. override func viewDidLoad()
2.     {
3.        super.viewDidLoad()
4.         
5.        setUpUserInterface();
6.  
7.    }
Every ActionScript developer will understand the code inside  setUpUserInterface():
   private func setUpUserInterface() -> Void
    {
01.     sliderOne.minimumValue = 0;
02.     sliderOne.maximumValue = 100;
03.      
04.     sliderTwo.minimumValue = 0;
05.     sliderTwo.maximumValue = 100;
06.      
07.     operatorButton.selectedSegmentIndex = 0;
08.  
09. }
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  event handlers , oops, actions, to all my controls, an  updateAll()  function uses a switch statement, again almost identical to AS3, to display the calculation depending on the sliders and segmented button (and display in red, if the value is negative):
 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.

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Simon Gladman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}