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

2019-02-28 08:50发布

问题:

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:

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))!)
}