我试图将一些代码从一种方法重构为它自己的方法,但编译器在提示。
此代码在较长的方法中运行良好
let aboutLocation = self.locationWords.contains(where: {$0.caseInsensitiveCompare((newmessage)!) == .orderedSame})
if (aboutLocation) {
self.startLocationServices()
}
当我尝试将代码放入其自己的方法中时,如下所示,它会给出错误消息:Extraneous argument label 'where' in call 并建议我删除该词。
func startLocationServicesIfLocation(newmessage:String){
let aboutLocation = self.locationWords.contains(where: {$0.caseInsensitiveCompare((newmessage)!) == .orderedSame})
if (aboutLocation) {
self.startLocationServices()
}
}
为什么一个方法内部与另一个方法不同
最佳答案
错误具有误导性。
在函数中,参数 newmessage
是非可选的,因此您必须删除感叹号(以及括起来的括号 – 也包括 if
条件 – 无论如何)。
let aboutLocation = self.locationWords.contains(where: {$0.caseInsensitiveCompare(newmessage) == .orderedSame})
if aboutLocation { ...
但您确实可以使用尾随闭包语法省略 where
参数标签
let aboutLocation = locationWords.contains{ $0.caseInsensitiveCompare(newmessage) == .orderedSame }
关于ios - 包含在 Swift 中放置在自己的方法中时语法不同的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52932556/