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)
}

以下是遵守此协议的协议
DoubleFloat, 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
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容