Swift 2 - Search in string and sum the numbers

2019-03-04 03:26发布

问题:

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:

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