无法使用类型为swift 2.0的参数列表调用reduce [英] cannot invoke reduce with an argument list of type swift 2.0

查看:95
本文介绍了无法使用类型为swift 2.0的参数列表调用reduce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将swift 1.2升级到swift 2.0时,会发生以下错误

When i upgrade swift 1.2 to swift 2.0 following error occurs

无法使用类型为

cannot invoke reduce with an argument list of type

这是代码

let escaped = reduce(string, "") { string, character in
    string + (character == mark ? "\(mark)\(mark)" : "\(character)")

能帮我解决这个问题吗

推荐答案

reduce()是用于数组之类的集合的方法,因此您必须在可以使用的characters属性访问的字符列表中调用它字符串,而不是整个字符串本身:

reduce() is a method for collections such as arrays so you have to call it on the list of the characters you can access using the characters property of the string, not on the whole string itself:

let escaped = string.characters.reduce("") { string, character in
    string + (character == mark ? "\(mark)\(mark)" : "\(character)")
}

这篇关于无法使用类型为swift 2.0的参数列表调用reduce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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