为什么';swift编译器有时不接受速记参数名吗?
数组是:为什么';swift编译器有时不接受速记参数名吗?,swift,swift2,Swift,Swift2,数组是:var closestAnnotations:[MKAnnotation] 我想知道为什么swift编译器不接受: let closestStationAnnotations = closestAnnotations.filter({ $0.dynamicType === StationAnnotation.self }) 无法将类型()->Bool的值转换为预期的参数类型(MKAnnotation)->Bool 但接受: let closest
var closestAnnotations:[MKAnnotation]
我想知道为什么swift编译器不接受:
let closestStationAnnotations = closestAnnotations.filter({
$0.dynamicType === StationAnnotation.self
})
无法将类型()->Bool的值转换为预期的参数类型(MKAnnotation)->Bool
但接受:
let closestStationAnnotations = closestAnnotations.filter({
(annotation : MKAnnotation) -> Bool in
annotation.dynamicType === StationAnnotation.self
})
我一直在试用不同版本的代码(使用Xcode 7)。修复方法很明显,使用
let closestStationAnnotations = closestAnnotations.filter({
$0 is StationAnnotation
})
这是测试类型的正确方法,工作没有任何问题
我注意到有一些简单的代码可以消除错误
let closestStationAnnotations = closestAnnotations.filter({
print("\($0)")
return ($0.dynamicType === StationAnnotation.self)
})
但是,这不起作用:
let closestStationAnnotations = closestAnnotations.filter({
return ($0.dynamicType === StationAnnotation.self)
})
如果注意到错误消息,编译器会将闭包视为(\ux)->Bool
这使我得出结论,表达式$0.dynamicType
以某种方式进行了优化
最有趣的是
let closestStationAnnotations = closestAnnotations.filter({
return true
})
将触发相同的错误
所以我认为有两个编译器错误:
[type]
时,()->Bool
应被视为(type)->Bool
$0.dynamicType
,这显然是错误的closestAnnotations
是如何声明的?请参见我的编辑!:)闻起来像虫子。我认为这与.dynamicType
不是一个,而是一个。但是我找不到任何合理的解释。我也感觉到与.dynamicType
的特殊性有关。问题是.dynamicType
不是成员。那可能是why@MatthieuRiegler是的,使用让arg=$0
然后arg.dynamicType==…
也解决了这个问题。它专门连接到$0.dynamicType
。我已经向苹果提交了一个bug。让我们看看他们是否会解决这个问题:)@MatthieuRiegler事实上,编译器不会推断返回true
作为有效的闭包,这让我更加惊讶。你看到了吗,在这种情况下,错误是不同的吗<代码>元组模式无法与非元组类型MKAnnotation的值匹配