10-Swift属性

2020-12-15  本文已影响0人  一抹相思泪成雨

1.属性

存储属性(Stored Property)
计算属性(Computed Property)
struct Circle {
    /// 存储属性
    var radius: Double
    /// 计算属性
    var diameter: Double {
        set {
            radius = newValue / 2
        }
        get {
            radius * 2
        }
    }
}
var circle = Circle(radius: 5)
print(circle.radius)
print(circle.diameter)
2.存储属性

关于存储属性,Swift有个明确的规定

3.计算属性
struct Circle {
   var radius: Double
   var diameter: Double {
       set(newDiameter) {
           radius = newDiameter / 2
       }
    get {
           radius * 2
        }
   }
}

// 只读计算属性: 只有get,没有set
struct Circle {
   var radius: Double
   var diameter: Double {
       get {
           radius * 2
     }
  }
}

4.枚举rawValue原理

enum TestEnum : Int {
    case test1 = 1, test2, test3
    var rawValue: Int {
        switch self {
        case .test1:
            return 10
        case .test2:
            return 11
        case .test3:
            return 12
        }
    }
}
print(TestEnum.test3.rawValue)

5.延迟存储属性

class Car {
    init() {
        print("Car init!")
    }
    func run() {
        print("Car is running!")
    }
}
class Person {
    lazy var car = Car()
    init() {
        print("Person init!")
    }
    func goOut() {
        car.run()
    }
}

let p = Person()
p.goOut()

class PhotoView {
    lazy var view: UIView = {
        let redView = UIView()
        redView.backgroundColor = .red
        return redView
    }()
}

延迟存储属性注意点

struct TestPoint {
    var x = 0
    var y = 0
    lazy var z = 0
}
var tp = TestPoint()
print(tp.z)

6.属性观察器

struct TestCircle {
    var radius: Double {
        willSet {
            print("willSet", newValue)
        }
        didSet {
            print("didSet", oldValue, radius)
        }
    }
    init() {
        self.radius = 1.0
        print("Circle init!")
    }
}
var testCircle = TestCircle()
testCircle.radius = 10.5
print(testCircle.radius)

7.全局变量、局部变量

var num: Int {
   get {
      return 10 
  }
   set {
       print("setNum", newValue)
    } 
}
num = 11 // setNum 11
print(num) // 10

func test() {
    var age = 10 {
     willSet {
         print("willSet", newValue)
     } 
      didSet {
          print("didSet", oldValue, age)
      }
   }
      age = 11
    // willSet 11
    // didSet 10 11
} 
test()

8.inout的本质

struct Shape {
    var width: Int
    var side: Int {
        willSet {
            print("willSide", newValue)
        }
        didSet {
            print("didSetSide",oldValue, side)
        }
    }
    var girth: Int {
        set {
            print("set girth")
            width = newValue / side
        }
        get {
            print("get girth")
            return width * side
        }
    }
    func show() {
        print("width=\(width), side=\(side), girth=\(girth)")
    }
}

func test(_ num: inout Int) {
    num = 20
}

var s = Shape(width: 10, side: 4)
test(&s.width)
s.show()
print("----------")
test(&s.side)
s.show()
print("----------")
test(&s.girth)
s.show()

9.类型属性

可以通过static定义类型属性,如果是类,也可以用关键字class

struct TestCar {
    static var count: Int = 0
    init() {
        TestCar.count += 1
    }
}

let c1 = TestCar()
let c2 = TestCar()
let c3 = TestCar()
print(TestCar.count)

10.类型属性细节

11.单例模式

public class FileManager {
//    public static let shared = FileManager()
    public static let shared = {
       return FileManager()
    }()
    private init() { }
}
上一篇下一篇

猜你喜欢

热点阅读