0%

swift-关键字

@objc 用于swift提供oc调用

  1. 在方法上加如

    @objc private func gotoLanguage(_ sender:UIButton){}

    因为Button加事件是OC调用 使用 btn.addTarget(self, action: #selector(self.gotoLanguage(_:)), for: .touchUpInside)

    数据类型 方法

    1. var 可变数据
    2. let不可变数据,常量
    3. typealias 给类型加别名 typealias joker = UIViewController
    4. as is String as NSString 转型 类型检查操作符 is let s = “joker”; if s is String {}
    5. inout 参见三,swift基础的方法
    6. lazy 懒加载
    7. static class 在func前加的关键字表示类型方法,都可以用于指定类方法.不同的是用class关键字指定的类方法可以被子类重写
    8. get{retrun value} set{newValue} 使用set 必须要用get 但是get 可以单独使用

      willSet { newValue} didSet {oldValue} set方法监听,不能与上面的共存,官方编译器不允许
    9. mutating 异变,结构体和枚举是值类型。默认情况下,值类型属性不能被自身的实例方法修改,mutating加在func前可进行修改
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      struct Point {    
      var x = 0.0, y = 0.0    
      mutating func moveBy(x deltaX: Double, y deltaY: Double) {   
           x += deltaX   
           y += deltaY    
      }
      }
      var somePoint = Point(x: 1.0, y: 1.0)
      somePoint.moveBy(x: 2.0, y: 3.0)
      print("The point is now at (\(somePoint.x), \(somePoint.y))")
      // prints "The point is now at (3.0, 4.0)"
      --------------------------------------------------------
      enum TriStateSwitch {    
      case off, low, high    
      mutating func next() {        
      switch self {        case .off:            self = .low        case .low:            self = .high        case .high:            self = .off        }    }}var ovenLight = TriStateSwitch.lowovenLight.next()// ovenLight is now equal to .highovenLight.next()// ovenLight is now equal to .off
  1. subscript 下标 下标脚本允许你通过在实例名后面的方括号内写一个或多个值对该类的实例进行查询, 如:someArray[index],someDictionary[key]

    自定义:get set可选,与类属性一样
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    subscript(index: Int) -> Int {
        get {        
    // return an appropriate subscript value here
        }    
    set(newValue) {        
    // perform a suitable setting action here
        }
    }
    --------------------------------------------------------
    struct TimesTable {
        let multiplier: Int
        subscript(index: Int) -> Int { 
           return multiplier * index
        }
    }
    let threeTimesTable = TimesTable(multiplier: 3)
    print("six times three is \(threeTimesTable[6])")
    // prints "six times three is 18"
    --------------------------------------------------------
    struct Matrix {
        let rows: Int, columns: Int
        var grid: [Double]
        init(rows: Int, columns: Int) {
            self.rows = rows
            self.columns = columns 
            grid = Array(repeating: 0.0, count: rows * columns)
        }
        func indexIsValid(row: Int, column: Int) -> Bool {
            return row >= 0 && row < rows && column >= 0 && column < columns
        }
        subscript(row: Int, column: Int) -> Double {
            get {
                assert(indexIsValid(row: row, column: column), "Index out of range")
                return grid[(row * columns) + column]
            }
            set {
                assert(indexIsValid(row: row, column: column), "Index out of range")
                grid[(row * columns) + column] = newValue
            }
        }
    }
    var matrix = Matrix(rows: 2, columns: 2)
    matrix[0, 1] = 1.5
    matrix[1, 0] = 3.2
    // 0.0 1.5
    // 3.2 0.0
  2. overide 重写var func class fuc subscript
  3. final 阻止子类对相应的属性,方法,下标等重写

语句

  1. switch 贯穿 fallthrough
  2. switch 分句检查 where
  3. 判断语句:1guard1 guard condition else{ statements; retrun} 与if语句类似,相比总有一个else语句

    else里面必需用returnbreakcontinue 或者 throw 返回

    它会让正常地写代码而不用把它们包裹进else 代码块,并且它允许你保留在需求之后处理危险的需求
  4. sdk版本号判定,系统新增 if #available(iOS 10, macOS 10.12, *) {}
  5. defer defer语句延迟执行直到当前范围退出
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    func processFile(filename: String) throws {
        if exists(filename) {
            let file = open(filename)
            defer {
                close(file)
            }
            while let line = try file.readline() {
                // Work with the file.
            }        
    // close(file) is called here, at the end of the scope.
        }
    }
    使用 defer语句来保证 open(_:)函数能调用 close(_:)
  1. 访问控制
    通过在实体的引入之前添加 `open` , `public` , `internal` , `fileprivate` ,或 `private`  修饰符来定义访问级别
    访问优先级如下:
    • open: 在Module内外都可以访问,继承,重载
    • public: 只在Module内可访问,继承,重载,外只能访问
    • internal :默认访问级别,本Module操作
    • fileprivate : 在当前swift文件里可操作
    • private :在当前类可操作