Swift 2 - 在字符串中搜索并对数字求和

时间:2015-07-04 20:38:13

标签: string search numbers sum swift2

我的旧代码是:

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

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

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

但现在我遇到了错误:Cannot invoke 'map' with an argument list of type '((_) -> _)'

如何修复它。

Link to old answer on Swift

1 个答案:

答案 0 :(得分:1)

split()函数的参数顺序由于某种原因搞砸了。它应该是:

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