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

旧街凉风 提交于 2019-12-01 13:14:48

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

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))!)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!