Int()和toInt()之间的快速差异

Ste*_*vik 3 swift

我需要简单的解释为什么我toInt()用于将字符串转换为整数.

我什么时候需要使用Int(variable)而不是variable.toInt()

nhg*_*rif 5

Swift Int没有接受a的构造函数String.

你想一个转换任何时间StringInt,你必须使用variable.toInt().

您只能使用Int(variable)if variable的类型在以下列表中:

  • Int
  • UInt8
  • Int8
  • UInt16
  • Int16
  • UInt32
  • Int32
  • UInt64
  • Int64
  • UInt
  • Float
  • Double
  • Float80
  • Int extension为其编写自定义并为其添加自定义的任何其他类型init.

对于任何其他类型,您必须使用可用的toInt()方法(如果存在),或者编写自己的方法.

此列表中的内容与不在此列表中的内容之间的主要区别在于,大多数情况下,Int可以准确地表示此列表中的所有内容.一个failable初始化不需要任何这些类型.

然而,当试图转换"Hello World!"为一个Int时,我们应该返回什么? StringtoInt()回报nil是因为String's toInt()的返回类型是Int?(Int可选).要做到同样的init,init必须是可以的(我在答案的底部发布了一个例子).

但是,如果要实现一个结构Rational来表示有理分数,那么扩展Int包含一个接受Rational数字的构造函数可能是有意义的:

extension Int {
    init(_ value: Rational) {
        // your implementation
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是可用构造函数的列表Int(您可以使用的情况Int(variable):

/// A 64-bit signed integer value
/// type.
struct Int : SignedIntegerType {
    /// Create an instance initialized to zero.
    init()
    /// Create an instance initialized to `value`.
    init(_ value: Int)    
    /// Creates an integer from its big-endian representation, changing the
    /// byte order if necessary.
    init(bigEndian value: Int)

    /// Creates an integer from its little-endian representation, changing the
    /// byte order if necessary.
    init(littleEndian value: Int)
    init(_builtinIntegerLiteral value: Builtin.Int2048)

    /// Create an instance initialized to `value`.
    init(integerLiteral value: Int)
}
Run Code Online (Sandbox Code Playgroud)
extension Int {
    init(_ v: UInt8)
    init(_ v: Int8)
    init(_ v: UInt16)
    init(_ v: Int16)
    init(_ v: UInt32)
    init(_ v: Int32)
    init(_ v: UInt64)

    /// Construct a `Int` having the same bitwise representation as
    /// the least significant bits of the provided bit pattern.
    ///
    /// No range or overflow checking occurs.
    init(truncatingBitPattern: UInt64)
    init(_ v: Int64)

    /// Construct a `Int` having the same bitwise representation as
    /// the least significant bits of the provided bit pattern.
    ///
    /// No range or overflow checking occurs.
    init(truncatingBitPattern: Int64)
    init(_ v: UInt)

    /// Construct a `Int` having the same memory representation as
    /// the `UInt` `bitPattern`.  No range or overflow checking
    /// occurs, and the resulting `Int` may not have the same numeric
    /// value as `bitPattern`--it is only guaranteed to use the same
    /// pattern of bits.
    init(bitPattern: UInt)
}
Run Code Online (Sandbox Code Playgroud)
extension Int {
    /// Construct an instance that approximates `other`.
    init(_ other: Float)
    /// Construct an instance that approximates `other`.
    init(_ other: Double)
    /// Construct an instance that approximates `other`.
    init(_ other: Float80)
}
Run Code Online (Sandbox Code Playgroud)

(您可以通过Int(0)在Swift中键入某个位置,右键单击并单击"跳转到定义" 来访问此列表.)

请注意,并非所有这些都是简单的Int(variable),其中一些必须像Int(littleEndian:variable)例如一样使用.

你可以使用的唯一方法Int(variable),其中variableString将添加自己的扩展Int:

extension Int {
    init?(_ s: String) {
        if let i = s.ToInt() {
            init(i)
        } else {
            init(0)
            return nil
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但我建议坚持下去variable.ToInt().