Swift Basic Syntax

Defining Variables

define the variable of Swift programming in a two way. let keyword are used to define constant variable (immutable variable). And var keywords are used to define mutable variables. If you are know about kotlin programming so that is similar.

let pi=3.1415 //immutable variables
var result=10 //mutable variables

Immutable variable, value are assign when it is define. And that value are never modified during of program execution. That means this is read only. And other side mutable variables are read and write.

In this case here are not mention that which type of value are stored by variable. In this situation compiler are provide data type using of variable value. In this process is called implicit type conversion. In given example pi is double type of variable and result variable is an integers.

When we are create a variable that values is assigning of in later. then in this case provide are enough information to variable. which type of variable?, And which type of value that will stored.

//age is an mutable integer variables
var age:Int

//set value of age
age=12

//display variable value
print("Your age is \(age)")

Define data type of after that of colon.

Output

Your age is 12

Property Observers

var info = 90 {

    willSet {
        print("Will set to \(info). It was previously \(info)")
    }
    didSet {
        print("Did set to \(info). It was previously \(info)")
    }
}
info=10

Output

Will set to 90. It was previously 90
Did set to 10. It was previously 10
var age = 10 {
    willSet(newAge) {
        print("Will set age to \(newAge) , it was \(age)")
    } 
    didSet(oldAge) {
        print("Did set age to \(age) , it was \(oldAge)")
    }
}
age=11

Output

Will set age to 11 , it was 10
Did set age to 11 , it was 10

Computed Properties

class Wallet{
    
    var rupee:Int = 100

    var dollar : Int {

        get{
            return rupee/74 //convert rupee to dollar
        }
        set{
            //modify dollar  to rupee
            rupee = (newValue*74)
        }
    }
}
let person = Wallet()

person.rupee = 74000

print("Dollar: \(person.dollar)") //get are execute

person.dollar=50 //set are execute

print("Rupee: \(person.rupee)") 

Output

Dollar: 1000
Rupee: 3700

Static Properties

class Fines{
    
    static var penaltyCharge : Int = 10

    init(_ newCharge:Int ){
        Fines.penaltyCharge=Fines.penaltyCharge+newCharge
    }
}

let withoutHelmet = Fines(100) //update fines
print("Helmet Charges : \(Fines.penaltyCharge)")

let withoutInsurance = Fines(200) //update fines


print("Helmet Charges + without Insurance : \(Fines.penaltyCharge)")

Output

Helmet Charges : 110
Helmet Charges + without Insurance : 310

Variable as Optional value

optional is an type safety mechanism there are generally used when variable can hold nil or specified values. nil is represent nothing. Optional variable are declared similar to normal variable only difference are included (?) question mark symbol after the data type. This symbol is indicating the variable or object can contain the value of nil or given specified data values.

var bugName : String? = nil //set nil value (no value)

bugName = "Memory Overflow"

print(bugName!) //Display optional value

Output

Memory Overflow

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved