Why do I get the error "Argument type 'String' does not conform to expected type 'sequence'

11,339

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

Solution 2

Swift 4 characters is deprecated, so above code would be like this:

let text : String = "12345"
var digits = [Int]()
for element in text {
    digits.append(Int(String(element))!)
}
Share:
11,339
Theodore.K
Author by

Theodore.K

Updated on June 30, 2022

Comments

  • Theodore.K
    Theodore.K almost 2 years

    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?