Monday, August 20, 2018

Swift Getting Started

1. Variable

var (mutable)
var age = 10
let (immutable)
let name = "Capung"
or with data type:
var age: Int = 10
let name: String = "Capung"
var isCool: Bool = true
let gender: Character = "M"

2. Print log

print()
contoh:
print("Name: ")
print(name) 
print("My Name is \(name)")

3.  If condition

if age > 10 {
    print("older than 10 years old")
} else {
    print("child boy")
}

4. Function

Void/ without return data
func calculate() {
    let a = 2
    let b = 2
    print(a + b)
}

with return data type
func calculate() -> Int {
    let a = 2
    let b = 2
    let c = 1
    return a + b - c
}

with parameter
func calculate(a: Int, b: Int) {
    print(a + b)
}
calculate(a: 1, b: 2)
 or
func calculate(_ a: Int, _ b: Int) {
    print(a + b)
}
calculate(1, 2)

5. Class

class Animal {
    let name: String = "Doggy"
    var age: String = "12 Month"

    func call() {
        print("bark bark bark")
    }

}
let Dog = Animal()
print(Animal.name)
Dog.call()

2018

2 comments:

  1. Why is the world's first titanium steel car - TITIAN ART
    This metal micro hair trimmer car will transport steel cars and bicycles to the world's first titanium steel car. suunto 9 baro titanium · · · · · · · · · In a similar way, the “iron car” titanium fat bike would transport titanium nail a bicycle, and one could transport the titanium max vehicle under a roof.

    ReplyDelete