defer

  • 将延迟至当前作用域结束之前执行
  • throws、return之前会执行

image.png

fatalError

  • 直接抛出错误,结束程序

溢出运算符Overflow Operator

&- 、 &+ 、&*

image.png

运算符重载

image.png

自定义运算符

image.png

中缀自定义

image.png
image.png

  1. infix operator +-+ : AdditionPrecedence
  2. struct Point {
  3. var x : Int, y: Int
  4. static func +-+(left:Point,right:Point) -> Int {
  5. return (left.x+right.x)-(left.y+right.y)
  6. }
  7. }

指针的pointee

image.png

获取指向某个变量的指针

image.png

获取指向堆空间实例的指针

image.png

通配符

image.png

重写switch case判断逻辑 ( ~= )

image.png
image.png

条件编译

image.png

打印

image.png

参考:https://docs.swift.org/swift-book/ReferenceManual/Attributes.html

KVO KVC

  • @objc dynamic 修饰对应属性 ```swift class WuObserver: NSObject {

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    1. print(change?[NSKeyValueChangeKey.oldKey]! )

    } } class People: NSObject {

    @objc dynamic var str = “init” var obs = WuObserver() var observation:NSKeyValueObservation?

    override init() {

    1. super.init()
    2. addObserver(obs, forKeyPath: "str", options: [.new,.old], context: nil)
    3. //或者通过block
    4. observation = observe(\People.str, options: .new, changeHandler: { (obser, change) in
    5. print(change,obser)
    6. })

    } }

  1. <a name="aSnpq"></a>
  2. # 利用关联对象,为class扩展储存属性
  3. ```swift
  4. class People {}
  5. extension People {
  6. private static var NAME_KEY: Void?
  7. var name : String {
  8. set {
  9. objc_setAssociatedObject(self, &People.NAME_KEY, newValue, .OBJC_ASSOCIATION_ASSIGN)
  10. }
  11. get {
  12. return (objc_getAssociatedObject(self, &People.NAME_KEY) as? String) ?? ""
  13. }
  14. }
  15. }
  16. let pp = People()
  17. pp.name = "yuan"
  18. print(pp.name)

多线程 加锁

image.png
image.pngimage.png