Swift笔记 - 08.可选链、协议

Swift笔记 - 08.可选链、协议

可选链

class Car { var price = 0 }
class Dog { var weight = 0 }
class Person {
    var name: String
    var dog: Dog = Dog()
    var car: Car? = Car()
    func age() -> Int { 18 }
    func eat() { print ("Person eat") }
    subscript (index: Int) -> Int { index }
}

var person: Person? = Person()
var agel = person!.age() // Int
var age2 = person?.age() // Int?
var name = person?.name // String?
var index = person? [6] // Int?

func getName()-> String{ "jack" }
//如果person是nil,不会调用getName()
person?.name = getName()
if let _ = person?.eat() { // ()?
    print("eat调用成功"
} else {
    print("eat调用失败"
}

var dog = person?.dog // Dog?
var weight = person?.dog.weight // Int?
var price = person?.car?.price // Int?
var scores = ["Jack": [86, 82, 84], "Rose": [79, 94, 81]]
scores ["Jack"]?[0] = 100
scores ["Rose"]? [2] += 10
scores ["Kate"]?[0] = 88

var num1: Int? = 5
num1? = 10 // Optional(10)
var num2: Int? = nil
num2? = 10 // nil

var dict: [String : (Int, Int) -> Int] = [
    " sum" : (+),
    "differencell" : (-)
]
var result = dict["sum"]? (10, 20) // Optional (30), Int?

协议(Protocol)

protocol Drawable {
func draw()
var x: Int { get set }
var y: Int { get }
subscript(index :Int)->Int { get set }
}

protocol Test1 { }
protocol Test2 { }
protocol Test3 { }
class TestClass : Test1, Test2, Test3 { }

协议中的属性

protocol Drawable {
    func draw()
    var x: Int { get set }
    var y: Int { get }
    subscript (index: Int) -> Int { get set }
}

class Person : Drawable {
    var x: Int = 0
    let y: Int = 0
    func draw() {
        print ("Person draw")
    }
    subscript(index: Int) -> Int {
        set { }
        get { index }
    }
}

class Person : Drawable {
    var x: Int
    {
        get {0}
        set { }
    }
    var y: Int { 0 }
    func draw() { print ("Person draw") }
    subscript(index: Int) -> Int {
        set { }
        get { index }
    }
}
protocol Drawable {
    static func draw()
}
class Person: Drawable {
    class func draw() {
        print("Person1 draw")
    }
}
class Person2: Drawable {
    static func draw() {
        print("Person2 draw")
    }
}
protocol Drawable {
    mutating func draw()
}
class Size: Drawable {
    var width: Int = 0

    func draw() {
        width = 10
    }
}
struct Point: Drawable {
    var x: Int = 10

    mutating func draw() {
        x = 10
    }
}
protocol Drawable {
    init(x: Int, y: Int)
}
class Point : Drawable {
    required init(x: Int, y: Int) { }
}
final class Size : Drawable {
    init(x: Int, y: Int) { }
}

protocol Livable {
    init(age: Int)
}
class Person {
    init(age: Int) { }
}
class Student : Person, Livable {
    required override init(age: Int) {
        super.init(age: age)
    }
}
protocol Livable {
    init()
    init?(age: Int)
    init!(no: Int)
}

class Person : Livable {
    required init() { }
    // required init! 0 { }
    required init? (age: Int) { }
    // required init! (age: Int) {}
    // required init(age: Int) { }
    required init! (no: Int) { }
    // required init?(no: Int) { }
    // required init(no: Int) { }
}

协议的继承与组合

protocol Runnable {
    func run()
}
protocol Livable : Runnable {
    func breath()
}
class Person: Livable {
    func breath() { }
    func run() { }
}
protocol Livable { }
protocol Runnable { }
class Person { }

//接收Person或者其子类的实例
func fn0(obi: Person){}
//接收遵守Livable协议的实例
func fn1(obj: Livable) { }
//接收同时遵守Livable、Runnable协议的实例
func fn2(obi: Livable & Runnable){}
//接收同时遵守Livable、Runnable协议、并且是Person或者其子类的实例
func fn3(obj: Person & Livable & Runnable) {}

typealias RealPerson = Person & Livable & Runnable
//接收同时遵守Livable、Runnable协议、并且是Person或者其子类的实例
func fn4(obj: RealPerson) { }

CaseIterable协议

enum Season : CaseIterable {
    case spring, summer, autumn, winter
}
let seasons = Season.allCases
print(seasons.count) // 4
for season in seasons {
    print (season)
} // spring summer autumn winter

CustomStringConvertible协议

class Person: CustomStringConvertible {
    var age: Int
    var name: String

    init(age: Int, name: String) {
        self.age = age
        self.name = name
    }

    var description: String {
        "age=\(age), name=\(name)"
    }
}
var p = Person(age: 10, name: "Jack")
print(p) // age=10, name=Jack

版权所有,转载请注明出处 luowei.github.io.