Swift 2-搜索字符串并求和 [英] Swift 2 - Search in string and sum the numbers

查看:135
本文介绍了Swift 2-搜索字符串并求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的旧代码是:

let comps = split(str, { $0 == "-" || $0 == " " }, maxSplit: Int.max, allowEmptySlices: false)

在Swift 2更新后,我的XCode 7将其修复为:

after update of Swift 2 my XCode 7 fix it to:

let comps = split(str.characters, { $0 == "-" || $0 == " " }, maxSplit: Int.max, allowEmptySlices: false).map { String($0) }

但是现在我有一个错误:Cannot invoke 'map' with an argument list of type '((_) -> _)'

but now I have an error: Cannot invoke 'map' with an argument list of type '((_) -> _)'

如何修复它.

链接到Swift上的旧答案

推荐答案

split()函数的参数顺序由于某种原因而混乱.应该是:

The order of arguments for split() function messed up for some reason. It should be:

let comps = split(str.characters, maxSplit: Int.max, allowEmptySlices: false) {
    $0 == "-" || $0 == " "
}.map {
    String($0)
}

这篇关于Swift 2-搜索字符串并求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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