• Getting started with Swift programming language

    19 February 2016

    Getting started with Swift programming language  

    apple-swift-logo
    Introduction to Swift : Tutorial 1

    Few days back, in a surprising move Apple introduced a new programming language called  ‘Swift’ to be a new platform for the development of iOS apps.

    Along with the introduction of Swift the XCode IDE is now upgraded to XCode 6.0, which will be the first version of Xcode to have the support for programming in Swift alongwith an updated version of iOS i.e iOS 8.0, and OS X i.e.  Mac OS X Yosemite.

    In this tutorial we will tell you how to write your first code in swift using Xcode 6.0(beta), and will discuss some key new feature like ‘playground’

    Playground in Xcode 6.0

    ‘Playground’ is a new feature in xcode6.0 where we can test our code live as we write it without the need to compile and run it again and again. The ‘playground’ file has a extension of ‘.playground’. There are 2 ways to create a ‘playground’ file:

    • Directly from the intro screen as we open the Xcode.

    Welcome to playground

    • From within the working project

    New file from within appselect playground filecreating playground inside code

    Once you have the ‘.playground’ file you can write whatever test code you want to test inside the file. Here’s an example:

    playground running viewautoview images   Tapping on the quicklook button shows the value the variable holds, can also be used for UIView and subclass objects. Tapping on value button displays the console output. You can playaround with the values and expressions in the file ‘Lesson1.playground’, which is a part of the sample code provided, and a great way to explore swift. We have already provided some sections on the following:

    •  example1: working with strings 
    • example2: working with basic calculations
    • example3: working with arrays and dictionary
    • example4: working with ‘Optional’ variables
    • example5: working with ‘switch’ and ‘for’ loops
     

    Creating a sample application in Swift

    In this step we will be creating a simple sample application with a ‘list’ table and a details screen. This application basically does the following:

    1. Read GOT characters from a file (GOTCharacters.plist);  display them as list on a tableView in viewController.swift.
    2. When user selects a character from the table display his/her details in another controller
      DetailViewController.swift

    Step1: Creating a New Sample Project in Swift

    Here’s step by step screenshots on how do we do that…

    Creating new project in xcode6Selecting project typeNaming ur new project

    Step2: Creating the model, view, and controller classes for the app…

    Xcode 6.0 supports both old Objective-C classes as well as the new ‘swift’ class. You can use them both in your project. All ‘swift’ classes end with the extension ‘.swift’, and does not contain any ‘.h’ or ‘.m’ file.                                                                                                                                        To see how to create classes for your application see the screenshots below…

    New class create type New file options

    Step3: Configuring the storyboard 

    The storyboard has 2 view controllers: a rootView controller of class ‘ViewController.swift’, and a detail view controller of class ‘DetailViewController.swift’

    XCode6.0 has a new feature called ‘adaptive’ UI that enables us to configure the same view in the nib for various device configurations; using this we can use the same storyboard for both iPad and iPhone devices. The adaptive feature is enabled by selecting on ‘enable size classes’ in Storyboard’s  interface builder…

    App main storyboardAdaptive views in storyboard2

    You can separately configure the iPad and iPhone Nib by tapping on the ‘Assistant Editor -> Preview’

    Adaptive views in storyboard

    Once we have the storyboard set up, we then connect the IBOutlets from the storyboard individual elements to the respective classes by going to ‘Assistant Editor -> Automatic’

    Storyboards and IBOutlets

    Step4: Writing the code in swift in the respective classes for the desired operation…  

    The swift syntax and style is more similar to javascript and other scripting languages than Objective-C. It simplifies a lot of things for the developers, and saves time by allowing us to do the same task using lesser lines of code than it would have been possible in Objective-C.                          The only problem I faced till now from the time I started learning to code in swift is the concept of ‘Optional’ variables, rest all looks to me well taken care of.

    But I guess every developer has his own opinion on things…
    Here’s an example of how the swift code looks like…

    swift code screenshot1swift code screenshot2


Comments

Comments closed on this post.