为什么我得到错误“参数类型'字符串'不符合预期类型'序列' [英] Why do I get the error "Argument type 'String' does not conform to expected type 'sequence'

查看:129
本文介绍了为什么我得到错误“参数类型'字符串'不符合预期类型'序列'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户输入从textField转换为数组。
我按照此处提供的代码 https://stackoverflow.com/a/27501398

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

然后我收到此错误:

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

我做错了什么?

推荐答案

似乎从Swift 2.0开始, String 不再符合 SequenceType 。如果你真的喜欢函数式编程,你可以解决这个问题。但是,这里没有必要这么花哨:

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

这篇关于为什么我得到错误“参数类型'字符串'不符合预期类型'序列'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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