美文网首页
swift常用协议

swift常用协议

作者: 想聽丿伱說衹愛我 | 来源:发表于2020-12-02 11:04 被阅读0次
  • RawRepresentable
    可通过XX(rawValue:)初始化,常用于struct,将结构体当成枚举使用。
struct ABC: RawRepresentable {
    var rawValue: Int
    
    init?(rawValue: Int) {
        self.init()
        self.rawValue = rawValue
    }
    
    init() {
        self.rawValue = 0
    }
    
    //enum
    static let a = ABC(rawValue: 10)
    static let b = ABC(rawValue: 11)
    static let c = ABC(rawValue: 12)
    static let d = ABC(rawValue: 13)
}

        let empty = ABC()                    // 0
        let a = ABC(rawValue: 10)            //10
        let b = ABC.b                        //11
        if a?.rawValue == b?.rawValue {
            print(true, a!.rawValue, b!.rawValue)
        } else {
            print(false, a!.rawValue, b!.rawValue)
        }

简写(建议)

struct ABC: RawRepresentable {
    var rawValue: Int
    
    //enum
    static let a = ABC(rawValue: 10)
    static let b = ABC(rawValue: 11)
    static let c = ABC(rawValue: 12)
    static let d = ABC(rawValue: 13)
}

        let a = ABC(rawValue: 10)       //10
        let b = ABC.b                   //11
        if a == b {
            //可直接用==判断
            print(true, a.rawValue, b.rawValue)
        } else {
            print(false, a.rawValue, b.rawValue)
        }

当枚举为Int Float String等时,会自动使用该协议

enum ABC : Int {
    case a = 0, b, c, d, e
}

let a = ABC(rawValue: 0) //可直接使用XX(rawValue:)初始化
let b = ABC.b
  • OptionSet
    常用于struct,将结构体当成options使用。
struct ABC: OptionSet {
    var rawValue: Int
    
    //options
    static let a = ABC(rawValue: 1 << 0)
    static let b = ABC(rawValue: 1 << 1)
    static let c = ABC(rawValue: 1 << 2)
    static let d = ABC(rawValue: 1 << 3)
}

        let a = ABC(rawValue: 10)
        let b = ABC.b
        let ab: ABC = [.a, .b]
        var empty: ABC = []
        let abc = empty.insert([.a, .b, .c])
        print(a == b)//false
        print(a.rawValue == b.rawValue)//false
        print(empty == abc.memberAfterInsert)//true
        print(ab.contains(.a))//true
  • CaseIterable
    常用于枚举,使用allCases访问枚举中的元素
enum ABC: CaseIterable {
    case a, b, c, d ,e
}

let cases = ABC.allCases //可获取枚举中全部元素
  • ExpressibleByNilLiteral
    可通过nil初始化
class ABC: ExpressibleByNilLiteral {
    
    required init(nilLiteral: ()) {
        print("nil")
    }
}

let a: ABC = nil
// 输出nil
  • ExpressibleByIntegerLiteral
    可通过整数初始化
class ABC: ExpressibleByIntegerLiteral {
    var number = 0
    
    required init(integerLiteral value: Int) {
        self.number = value
    }
    
    typealias IntegerLiteralType = Int
}

let a: ABC = 10
print(a.number)
//输出10
  • ExpressibleByFloatLiteral
    可通过浮点初始化,使用方法同ExpressibleByIntegerLiteral

  • ExpressibleByBooleanLiteral
    可通过布尔初始化,使用方法同ExpressibleByIntegerLiteral

  • ExpressibleByUnicodeScalarLiteral
    可通过统一码初始化,使用方法同ExpressibleByIntegerLiteral

  • ExpressibleByExtendedGraphemeClusterLiteral

  • ExpressibleByStringLiteral
    可通过字符串初始化,使用方法同ExpressibleByIntegerLiteral

  • ExpressibleByArrayLiteral
    可通过数组初始化,使用方法同ExpressibleByIntegerLiteral

  • ExpressibleByDictionaryLiteral
    可通过字典初始化,使用方法同ExpressibleByIntegerLiteral

  • ExpressibleByStringInterpolation
    可通过”\(xx)”初始化

class ABC: ExpressibleByStringInterpolation {
    var string = ""
    
    typealias StringLiteralType = String
    
    required init(stringLiteral value: String) {
        string = value
    }
}

let number = 10
let a: ABC = "abc\(number)defg"
print(a.string)
//输出abc10defg
  • StringInterpolationProtocol
    ExpressibleByStringInterpolation的详细实现协议
        //DefaultStringInterpolation中实现了该协议
        let name = "baidu"
        let phone = "13112345678"
        var interpolation = DefaultStringInterpolation(literalCapacity: 26, interpolationCount: 2)
        interpolation.appendLiteral("my name is ")
        interpolation.appendInterpolation(name)
        interpolation.appendLiteral(", my phone is ")
        interpolation.appendInterpolation(phone)
        interpolation.appendLiteral(".")
        let a = String(stringInterpolation: interpolation)
        print(a)
        //相当于"my name is \(name), my phone is \(phone)."
  • StringProtocol
    字符串协议

  • BinaryInteger
    整数协议

  • BinaryFloatingPoint
    浮点协议

  • CustomStringConvertible
    可使用description

  • CustomDebugStringConvertible
    可使用debugDescription

  • LosslessStringConvertible
    可使用init?(_ description: String)初始化,并给description赋值

  • Equatable
    可使用==和!=

  • Comparable
    可使用< 、<=、>、>=、…、..<

  • Hashable
    可哈希

  • Encodable
    可编码

  • Decodable
    可解码

相关文章

网友评论

      本文标题:swift常用协议

      本文链接:https://www.haomeiwen.com/subject/ikjywktx.html