Int()和toInt()之间的快速区别

斯蒂维克

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

我什么时候需要使用Int(variable)代替variable.toInt()

写作

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

每当您要将a转换String为an时Int,都必须使用variable.toInt()

您只能使用Int(variable)如果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)
}

(您可以通过Int(0)在Swift中的某个位置键入内容,右键单击并单击“跳转到定义”来获得此列表。)

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

您可以Int(variable)在where使用的唯一方法variableString将您自己的扩展名添加到Int

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Int()和toInt()之间的快速区别

来自分类Dev

C#中'(int)a','a as int'和Convert.ToInt32(a)之间的区别

来自分类Dev

C#中'(int)a','a as int'和Convert.ToInt32(a)之间的区别

来自分类Dev

Int ...和Int []之间的区别

来自分类常见问题

快速“前提”和“断言”之间的区别?

来自分类Dev

UIButton之间的区别!和UIButton?在快速的iOS

来自分类Dev

快速的CBUUID和NSUUID之间的区别

来自分类Dev

快速设置-“ randomElement”和“ first”之间的区别

来自分类Dev

int * a和char * a之间的区别?

来自分类Dev

char []和int []之间的区别

来自分类Dev

int * a和int * a = new int之间的区别

来自分类Dev

void(int)和void(*)(int)之间的区别

来自分类Dev

const int和int文字之间的区别

来自分类Dev

int和new int()之间的区别

来自分类Dev

在UInt和Int之间快速转换

来自分类Dev

Convert.ToInt16和(Int16)有什么区别

来自分类Dev

size_type和int之间的区别

来自分类Dev

MySQL中INT和UUID之间的区别

来自分类Dev

“ void main”和“ int main”之间的区别

来自分类Dev

int a [9]和a [3] [3]之间的区别

来自分类Dev

快速关闭中返回Void和()之间的区别

来自分类Dev

慢系统调用和快速系统调用之间的区别

来自分类Dev

UIButton之间的区别!和UIButton?在快速的IOS中

来自分类Dev

Seq [Int]和List [Int]之间的区别和转换

来自分类Dev

0,int()和int {}之间有什么区别?

来自分类Dev

OS XC中的int(*)(...)和int(^)(...)之间的区别?

来自分类Dev

类型之间的区别-C中的int *和int * [100]?

来自分类Dev

“ List <int> [,]”和“ List <List <int >>”之间的区别

来自分类Dev

((int)a)和(int(a))之间有什么区别?

Related 相关文章

热门标签

归档