问题描述
Swift 1.2 最新升级后,我不知道如何将一行文本拆分为单词.我曾经这样做过:
After the latest upgrade of Swift 1.2, I can't figure out how to split a line of text into words. I used to do this:
let bits = split(value!, { $0 == " "}, maxSplit: Int.max, allowEmptySlices: false)
但这不再有效,因为...
But that no longer works, because...
Cannot invoke 'split' with an argument list of type '(String, (_) -> _, maxSplit: Int, allowEmptySlices: Bool)'
嗯,好吧,即使我可以最后建造?好吧,无论如何,让我们尝试...
Ummm, ok, even though I could last build? Well whatever, let's try...
let bits = split(value!, { $0 == " "})
好吧,我能想到的所有其他版本最终都会说:
Well that and every other version I can think of ends up saying:
Missing argument for parameter 'isSeparator' in call
让我们听听新编程语言的 Beta 测试!耶!
Let's hear it for beta-testing new programming languages! Yay!
有人知道 1.2 的正确秘诀吗?
Anyone know the correct secret sauce for 1.2?
推荐答案
Swift 1.2 中的参数顺序似乎发生了变化:
It seems that the order of the parameters changed in Swift 1.2:
let bits = split(value!, maxSplit: Int.max, allowEmptySlices: false,
isSeparator: { $0 == " "})
或者,使用默认值:
let bits = split(value!, isSeparator: { $0 == " "})
谓词现在是最后一个参数,需要一个外部参数名称 isSeparator
因为它前面是可选参数.
The predicate is now the last parameter and requires an externalparameter name isSeparator
because it is preceded by optional parameters.
这个改动的好处是可以使用尾随闭包语法:
The advantage of this change is that you can use the trailing closuresyntax:
let bits = split(value!, maxSplit: Int.max, allowEmptySlices: false) { $0 == " " }
或
let bits = split(value!) { $0 == " " }
这篇关于split 现在抱怨缺少“isSeparator";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!