转换字符串数组为int数组斯威夫特2? [英] Convert String Array into Int Array Swift 2?

查看:115
本文介绍了转换字符串数组为int数组斯威夫特2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[X code 7.1,iOS的9.1]

[Xcode 7.1, iOS 9.1]

我有一个数组: VAR数组:[字符串] = [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.

我怎么能转换字符串数组斯威夫特2的内部阵列?

How can I convert a String array into an Int Array in Swift 2?

我试过几个环节,都没有工作,所有的人给了我一个错误。大多数从链接code是德preciated或尚未更新迅速2,如 toInt()方法。

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.

推荐答案

使用地图函数

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

在像的UIViewController A类用

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]
}

这篇关于转换字符串数组为int数组斯威夫特2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆