Protocols-ExpressibleByIntegerLiteral

public protocol ExpressibleByIntegerLiteral {

    /// A type that represents an integer literal.
    ///
    /// The standard library integer and floating-point types are all valid types
    /// for `IntegerLiteralType`.
    associatedtype IntegerLiteralType : _ExpressibleByBuiltinIntegerLiteral

    /// Creates an instance initialized to the specified integer value.
    ///
    /// Do not call this initializer directly. Instead, initialize a variable or
    /// constant using an integer literal. For example:
    ///
    ///     let x = 23
    ///
    /// In this example, the assignment to the `x` constant calls this integer
    /// literal initializer behind the scenes.
    ///
    /// - Parameter value: The value to create.
    init(integerLiteral value: Self.IntegerLiteralType)
}

以下是遵守此协议的协议
Double,Float, Float80, Numeric

这个协议是为了让遵守其协议的类型, 接受int数据的赋值。

举例:

// int 类型数据'
let cookieCount = 12

// 使用int类型的1赋值了 double 类型的值
let redPercentage: Double = 1
// redPercentage == 1.0

Double 举例:

extension Double : ExpressibleByIntegerLiteral {

    /// Creates an instance initialized to the specified integer value.
    ///
    /// Do not call this initializer directly. Instead, initialize a variable or
    /// constant using an integer literal. For example:
    ///
    ///     let x = 23
    ///
    /// In this example, the assignment to the `x` constant calls this integer
    /// literal initializer behind the scenes.
    ///
    /// - Parameter value: The value to create.
    public init(integerLiteral value: Int64)

    /// A type that represents an integer literal.
    ///
    /// The standard library integer and floating-point types are all valid types
    /// for `IntegerLiteralType`.
    public typealias IntegerLiteralType = Int64
}

你可能感兴趣的:(Protocols-ExpressibleByIntegerLiteral)