Swift 2 - Search in string and sum the numbers

2019-03-04 03:08发布

My old code was:

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

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

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

How to fix it.

Link to old answer on Swift

1条回答
Summer. ? 凉城
2楼-- · 2019-03-04 03:35

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)
}
查看更多
登录 后发表回答