Swift: Simple inheritance with a method

This entry is part 21 of 41 in the series Swift

This is an inherited class with a call to the parent’s method.

class Person {
    var name = ""
    var age = 0
    
    init(){
        self.name = ""
        self.age = 0
    }
    
    init(name:String,age:Int){
        self.name = name
        self.age = age
    }
    
    func printIntro(){
        print("My name is \(self.name) My age is \(self.age)")
    }

}


class Student : Person {
    
}

var student = Student(name: "Taylor Swift", age: 20)

student.printIntro()

swift

Swift: A struct with a closure

This entry is part 22 of 41 in the series Swift

When Swift was introduced last year by Apple in one of its events, it mentioned about closure, and I have no idea what that was.  I have looked at Swift for a couple of days and I am still a bit confused about how it is going to be used.

Closure is a piece of very concise code and it is actually quite easy to construct as shown in this example.

struct Square{
    var width = 0
    var height = 0
    
    func area()->Int{
        return width*height
    }
    
    func printArea(){
        print("area \(area())")
    }
    
    //closure
    func repeatTill(count count:Int , _ delimiter:String) {
        for _ in 1...count {
            print(delimiter)
            printArea()
        }
    }
    
}

var square = Square(width: 10, height: 5)

square.repeatTill(count:3,"***")

swift

Swift: Else if

This entry is part 23 of 41 in the series Swift

The if, else if statements in Swift.

func numvalue(num: Int) ->String {
    
    if num > 0 {
        return "positive"
    } else if num == 0 {
        return "zero"
    } else {
        return "negative"
    }
}

print(numvalue(-7))

swift

Swift Operators

This entry is part 24 of 41 in the series Swift

Arithmetic, comparison and logical operators.

// Swift All Operators

var x : Int = 10
var y : Bool = false

// Arithmetic Operators
x = x + 10; print("X = \(x)")     // (+)  Operator example
x = x - 15; print("X = \(x)")     // (-)  Operator example
x = x * 10; print("X = \(x)")     // (*)  Operator example
x = x / 5;  print("X = \(x)")     // (/)  Operator example
x = x % 10; print("X = \(x)")     // (%)  Operator example
x++;        print("X = \(x)")     // (++) Operator example
--x;        print("X = \(x)")     // (--) Operator example
print ("")
// Comparison Operators
x == 0  ?   print("True") :   print("False")       // Equla to operator
x != 0  ?   print("True") :   print("False")       // Not equal to operator
x < 0   ?   print("True") :   print("False")       // Less than operator
x > 0   ?   print("True") :   print("False")       // Greater than operator
x <= 0  ?   print("True") :   print("False")       // Less equal operator
x >= 0  ?   print("True") :   print("False")       // Greater equal operator
print ("")
// Logical Operators
x == 0 && x < 1 ?   print("True") :   print("False")     // And Operator
x == 0 || x == 1 ?  print("True") :   print("False")     // Or Operator
!y == false   ?     print("True") :   print("False")     // Not Operator

swift