美文网首页
Protocols-ExpressibleByIntegerLi

Protocols-ExpressibleByIntegerLi

作者: sasky2008 | 来源:发表于2020-12-10 10:18 被阅读0次
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
}

相关文章

网友评论

      本文标题:Protocols-ExpressibleByIntegerLi

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