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

查看:12
本文介绍了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) }

但现在我有一个错误:无法使用类型为((_) -> _)"的参数列表调用map"

如何解决.

链接到 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天全站免登陆