在swift中将用户输入转换为Ints数组 [英] convert user input to array of Ints in swift

查看:283
本文介绍了在swift中将用户输入转换为Ints数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个简单的iOS游戏,在swift中学习编程。
用户在文本字段(键盘类型数字键盘,如果重要)中输入一个4位数字,我的程序应该取4位数字并将每个数字放在一个数组中。
基本上我想要像

  userInput =1234
/ pre>

成为

  inputArray = [1,2, 3,4] 



我知道将字符串转换为字符数组在swift中非常容易



  var text:String =BarFoo
var arrayText = Array(text)
//返回[ B,a,r,F,o,o]

我的问题是我需要我的数组填充整数,而不是字符。
如果我将用户输入转换为一个Int,它变成一个单一的数字,所以如果用户输入1234,数组填充[1234]而不是[1,2,3,4]



所以我试图将用户输入视为字符串,创建一个字符数组,然后循环遍历该数组的元素,将它们转换为Ints并将它们放入第二个数组,如:

  var input:String = textField.text 
var inputArray = Array(input)
var intsArray = [Int]()

for var i = 0; i< inputArray.count; i ++ {
intsArray [i] = inputArray [i] .toInt()
}

但它不编译并给我错误:'Character'没有成员命名为'toint'



我做错了什么?

解决方案

您可以使用:

  let text:String =123a
let digits = Array(text).map {String($ 0).toInt()! }
//崩溃如果任何字符不是int

无效。



您可以通过检查 toInt()的结果来验证:

  let text:String =1234
var digits = Array(text).reduce([Int](),combine:{ digit,optionalDigit) - > [Int] in
if let digit = String(optionalDigit).toInt(){
digits.append(digit)
}
$ b b返回数字
})

如果countElements(text)== digits.count {
//所有数字有效
} else {
//有无效的数字
}


I'm trying to make a simple iOS game to learn programming in swift. The user inputs a 4 digits number in a text field (keyboard type number pad if that matters) and my program should take that 4 digits number and put each digit in an array. basically I want something like

userInput = "1234"

to become

inputArray = [1,2,3,4]

I know converting a string to an array of characters is very easy in swift

var text : String = "BarFoo"
var arrayText = Array(text)
//returns ["B","a","r","F","o","o"]

my problem is I need my array to be filled with Integers, not characters. If I convert the user input to an Int, it becomes a single number so if user enters "1234" the array gets populated by [1234] and not [1,2,3,4]

So I tried to treat the user input as a string, make an array of its characters and, then loop through the elements of that array, convert them to Ints and put them into a second array, like:

var input : String = textField.text
var inputArray = Array(input)
var intsArray = [Int]()

for var i = 0; i < inputArray.count ; i++ {
    intsArray[i] = inputArray[i].toInt()
}

but it doesn't compile and gives me the error: 'Character' does not have a member named 'toint'

What am I doing wrong?

解决方案

You could use:

let text : String = "123a"
let digits = Array(text).map { String($0).toInt()! }
// Crash if any character is not int

But it will crash if input is not valid.

You can validate by checking the result of toInt():

let text : String = "1234"
var digits = Array(text).reduce([Int](), combine: { (var digits, optionalDigit) -> [Int] in
    if let digit = String(optionalDigit).toInt() {
        digits.append(digit)
    }

    return digits
})

if countElements(text) == digits.count {
    // all digits valid
} else {
    // has invalid digits
}

这篇关于在swift中将用户输入转换为Ints数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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