Swift difference between Int() and toInt()



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

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

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

任何时候要将String转换为Int都必须使用variable.toInt()

如果variable的类型在以下列表中,则只能使用Int(variable)

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

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

该列表中的内容和不在此列表中的东西之间的主要区别在于,在大多数情况下,Int可以准确地表示此列表中的所有内容。对于这些类型中的任何一种,都不需要可失败的初始值设定项。

然而,当尝试将"Hello World!"转换为Int时,我们应该返回什么?StringtoInt()返回nil,因为StringtoInt()的返回类型是Int?(可选的Int)。要在init中执行同样的操作,init必须是可故障的(我在答案的底部发布了一个示例)。

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

extension Int {
    init(_ value: Rational) {
        // your implementation
    }
}

以下是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)
}
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)
}
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)
}

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

注意,并不是所有的都是简单的Int(variable),其中一些必须像Int(littleEndian:variable)一样使用。

variableString的情况下,使用Int(variable)的唯一方法是将您自己的扩展添加到Int:

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

但我建议您坚持使用variable.ToInt()

Int(variable)是一个构造函数,它有一组类型,可以用来创建Int变量。

.toInt()类似于可以应用于任何数据类型(支持该扩展)的扩展,并且该数据类型不需要是Int构造函数支持的类型之一。如果数据类型知道如何将其自身转换为Int,则可以使用toInt()函数扩展其功能。

最新更新