Why do I get the error "Argument type 'String&

2019-02-28 09:17发布

I'm trying to convert user input from a textField to an array. I followed the code that was offered here https://stackoverflow.com/a/27501398

let someString : String = someTextField.text!
let someArray = Array(someString).map { String($0).toInt()! }

But then I get this error:

 Argument type "String" does not conform to expected type "Sequence"

What am I doing wrong?

1条回答
放荡不羁爱自由
2楼-- · 2019-02-28 09:30

It seems that as of Swift 2.0, String no longer conforms to SequenceType. You can work around this if you're really in love with functional programming. However, there's no need to get so fancy here:

let text : String = "12345"
var digits = [Int]()
for element in text.characters {
    digits.append(Int(String(element))!)
}
查看更多
登录 后发表回答