in IOS swift Apple ~ read.

Introduction of Swift

In this post we are going to have a dive into what Swift language is and the its feature.

What is Swift?

Swift is a object-oriented programming language introduced by Apple in 2014. It is an attempt to replace their previous language - Object C.

Swift features

There are a lot of features brought by Swift that I think are pretty amazing and useful. Let's dive into it for more details.

No Semicolons

let hello = 'hello'  
let world = 'world'  

There is no semicolon in Swift, just like Python which I think is pretty great. By focusing on the empty space of the code which will increase the readability of the code and remove some redundancy.

It won't cause errors if you add them.

Define variables

var name    = "well"    // String type  
var age     = 99        // Int type  
var weight  = 55.5      // Double type  
var isAlive = true      // Bool type  

We use var to define variable and we are not defining the type when we initial the variables but Swift will be smart enough to pick up the type based on its value.

Explicit Types and Constants

var name: String    = "well"    // String type  
var age: Int        = 99        // Int type  
var weight: Double  = 55.5      // Double type  
var isAlive: Bool   = true      // Bool type  

As you can see this time that we can explicitly specify the type for our variables as well.

// Types 
String  
Int  
Double  
Float  
Bool  
UInt  
Character  

If you want to create a constant that you don't want to change ever. you need to use let keyword instead of var

let name = 'Well'  
name = 'Howdy' // This one will cause an error  

Managing Program Flow

Like many other programming languages that Swift has its ways of managing program flow.

if age > 18 {  
   // code
} else {
   // code
}

Parentheses are not needed, but curly braces are.

while age > 18 {  
   // code
}
repeat {  
   // code
} while age > 18

String Interpolation

Say if you want to put variables into your string you can do the following.

let message = "The age of me is \(age)"  

Swift Optionals

Like we mentioned about that we can define types for our variables. But it is not compulsory.

var name: String?    = "well"    // String type  
var age: Int?        = 99        // Int type  
var weight: Double?  = 55.5      // Double type  
var isAlive: Bool?   = true      // Bool type  

Creating and calling functions

It is pretty common the way Swift creates or call functions.

func displayMsg(Msg: String) -> String {  
    return "Here is your \(Msg)"
}

let msg = displayMsg(Msg: "howdy") // Here is howdy  

First we use func to indicate we are going to create a function and give it a name of displayMsg. And this function will receive one argument called Msg whose type is String. And it will return type of String as well.

Defining Classes and Creating Object

class Person {  
    // properties
    var name: String
    var age: Int
    var isAlive: Bool

    // initialiser
    init() {
        self.name    = "Well"
        self.age     = 99
        self.isAlive = true
    }

    // method
    func displayMsg(Msg: String) -> String {
        return "Here is your \(Msg)"
    } 
}

var well = Person()  

We will use class keyword first to create a class. Give it a name of Person. We will define some properties for Person class. It is a little bit difference to initialise the class compared to other languages that are using constructor. Swift use init() to initialise class. And unlike other programming languages that are using new keyword to create an instance of the class, in Swift we don't do it.

End

This is a rather crash course of the Swift language. Surely there are a lot of stuff we haven't covered yet. But hope this will be the first step for you going into it. As always if you have any opinions or ideas please leave a comment below. Thanks.

comments powered by Disqus