简体   繁体   English

将字符串数组转换为整数数组 Swift 2?

[英]Convert String Array into Int Array Swift 2?

[Xcode 7.1, iOS 9.1] [Xcode 7.1, iOS 9.1]

I have an array: var array: [String] = ["11", "43", "26", "11", "45", "40"]我有一个数组: var array: [String] = ["11", "43", "26", "11", "45", "40"]

I want to convert that (each index) into an Int so I can use it to countdown from a timer, respective of the index.我想将它(每个索引)转换成一个 Int,这样我就可以用它从一个计时器开始倒计时,对应于该索引。

How can I convert a String array into an Int Array in Swift 2?如何在 Swift 2 中将String数组转换为Int数组?

I've tried several links, none have worked and all of them have given me an error.我尝试了几个链接,但没有一个有效,而且所有链接都给了我一个错误。 Most of the code from the links is depreciated or hasn't been updated to swift 2, such as the toInt() method.链接中的大部分代码已贬值或尚未更新为 swift 2,例如toInt()方法。

Use the map function使用map功能

let array = ["11", "43", "26", "11", "45", "40"]
let intArray = array.map { Int($0)!} // [11, 43, 26, 11, 45, 40]

Within a class like UIViewController use在像UIViewController这样的类中使用

let array = ["11", "43", "26", "11", "45", "40"]
var intArray = Array<Int>!

override func viewDidLoad() {
  super.viewDidLoad()
  intArray = array.map { Int($0)!} // [11, 43, 26, 11, 45, 40]
}

If the array contains different types you can use flatMap (Swift 2) or compactMap (Swift 4.1+) to consider only the items which can be converted to Int如果数组包含不同的类型,您可以使用flatMap (Swift 2) 或compactMap (Swift 4.1+) 只考虑可以转换为Int

let array = ["11", "43", "26", "Foo", "11", "45", "40"]
let intArray = array.compactMap { Int($0) } // [11, 43, 26, 11, 45, 40]

Swift 4, 5:斯威夫特 4、5:

Use compactMap with cast to Int, solution without '!'.使用compactMap 与cast to Int,解决方案没有'!'。

let array = ["1","foo","0","bar","100"]
let arrayInt = array.compactMap { Int($0) }

print(arrayInt)
// [1, 0, 100]

i suggest a little bit different approach我建议有点不同的方法

let stringarr = ["1","foo","0","bar","100"]
let res = stringarr.map{ Int($0) }.enumerate().flatMap { (i,j) -> (Int,String,Int)? in
    guard let value = j else {
        return nil
    }
    return (i, stringarr[i],value)
}
// now i have an access to (index in orig [String], String, Int) without any optionals and / or default values
print(res)
// [(0, "1", 1), (2, "0", 0), (4, "100", 100)]

Swift 4, 5:斯威夫特 4、5:

The instant way if you want to convert string numbers into arrays of type int (in a particular case i've ever experienced):如果您想将字符串数字转换为 int 类型的数组(在我曾经经历过的特定情况下)的即时方法:

let pinString = "123456"
let pin = pinString.map { Int(String($0))! }

And for your question is:对于你的问题是:

let pinArrayString = ["1","2","3","4","5","6"]
let pinArrayInt = pinArrayString.map { Int($0)! }

A slightly different example一个稍微不同的例子

let digitNames = [0: "Zero", 1: "One", 2:"Two", 3: "Three",
                  4:"Four",5:"Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine", 10:"Ten"

]

let numbers = [16,58,510]
let strings = numbers.map { (number) -> String in
    var number = number
    var output = ""
    repeat {
        output = digitNames[number % 10]! + output
        number /= 10
        
    } while number > 0
            return (output)
}

print(strings)


// strings is inferred to be of type [String]

// its value is ["OneSix", "FiveEight", "FiveOneZero"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM