# 3/22 Nullable 函式呼叫 - Nullable Variable - function invoke - 語言上常用的基本型別 ## Nullable Variable ### kotlin ```kotlin= data class Cat { fun eat() } val kitty: Cat? = null kitty ?: run { // 如果 obj 是 null 的話,就做 {} 裡的事情 } kitty?.let { helloKitty -> helloKitty.eat() } kitty?.eat() let, also, apply, run, init val QQQQ = kitty?.let { it.eat() }?.also { it.toXXXX() } kitty?.apply { eat() } kitty?.also { Log.d("Zaccc", "XXXXX ${it}") } ``` ### Swift ```swift= class Cat { func eat() { // .... } } // 宣告一個變數 "kitty" 是 optional Cat 型別 var kitty: Cat? = nil if let cat = kitty { // 這邊就是 kitty **有**值的情況 cat.eat() } else { // 這邊就是 kitty **沒有**值的情況 cat.drink() } (cat != nil) ? cat?.eat() : cat?.drink() let goodCat: Bool = (cat != nil) ? true : false ``` ## function invoke - swift 有內外參數名,可以分開定義;kotlin 沒有 - 把 function 當作參數傳進 function - ~~typealias;kotlin 沒有~~ https://kotlinlang.org/docs/type-aliases.html ### Kotlin ```kotlin= class Person { fun update(model: Model, method: Method, (name:String)-> String) } val person = Person() person.update(Model(), Method()) { name -> when(name){ "wang" -> return "wangwang" else -> return "" } } ``` ### Swift ```swift= class Person { // byModel 外部參數名 // model 內部參數名 func update(byModel model: Model, byMethod method: ABCMethod) { model.foo() method.goo() } func eat(somthing: Food) { // ... } func drink(cup somthing: laverage) { // ... something.foo() } } let bob = Person() bob.eat(something: Hamburger()) bob.drink(cup: Starbuck()) bob.update(byModel: xxx, byMethod: ooo) // closure let handler: (String) -> String = { name in return "I am \(name)" } hanler("bob") // "I am bob" func foo(completionHanlder: (String) -> String) { ... completionHanlder("alice") } typealias MyStyle = (String) -> String func goo(completion: MyStyle) { completion("jenny") } typealias TimeInterval = Double let timeA: TimeInterval = 10.0 let timeB: Double = 20.0 ``` > `data class` 跟 `class` 有什麼差? `bean` *java* 跟 `data class` kotlin 比較像 ```swift= stuct Veicle { var driver: String mutating func changeDriver(newDriver: String) { driver = newDriver } } ``` ```swift= protocol Hashable { func hash() -> String } ``` ```swift= enum Direction { case north, west, east, south } let dir: Direction = .north let dir = Direction.north // rawValue enum Direction: Int { case north = 0 case west = 33 case east = 123 case south = 999 } let dir = Direction.north let num: Int = dir.rawValue // 0 ``` ```kotlin= enum class Direction { NORTH, // 0 WEST, // 1 EAST, // 2 SOUTH // 3 } val dir: Direction = Direction.NORTH val dir = Direction.NORTH val dir: Direction = 0 // compiler failed enum class Direction(val index: Int) { NORTH(999), // 999 WEST(998), // 998 EAST(997), // 997 SOUTH(995) // 995 } sealed class Animal { class Fish() data class Cat(val name: String) : Animal() object Dog() : Animal() } when () { Cat -> Dog -> else -> } ``` :::danger sealed class in Kotlin ??? ::: reference type / value type class AAA { } let helper = AAA() class PageA { let helper: AAA } class PageB { let helper: AAA } func main() { let helper = AAA() let pageA = PageA(helper: helper) let pageB = PageB(helper: helper) } 物件導向的繼承 data class, class, interface, enum class = sealed class enum class 不能繼承 enum class --- :::warning - 下次要準備給對方平台了解的基礎知識 - Kotlin: 有哪些基本型別、他們怎麼使用 - Swift: 有哪些基本型別、他們怎麼使用 - OOP 怎麼使用 - strategy pattern 範例 - OOP(object-oriented programming) -> POP (protocol-oriented programming) - Swift Singleton 怎麼建立?使用他? ::: |Swift|Kotlin| |---|---| |struct| data class | |class| class| | x | abstract class | |protocol| interface| |enum| enum class = sealed class | |optional| x |