Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么';swift编译器有时不接受速记参数名吗?_Swift_Swift2 - Fatal编程技术网

为什么';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的值匹配