Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为什么swift编译器有时不接受速记参数名称?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

数组是:var nearestAnnotations:[MKAnnotation] 我想知道swift编译器为什么不接受: let closestStationAnnotations = closestAnnotations.filter({ $0.dynamicType === StationAnnotation.self }) 无法转换类型(_)的值 – > Bool到
@H_301_0@
@H_301_0@
数组是:var nearestAnnotations:[MKAnnotation]

我想知道swift编译器为什么不接受:

let closestStationAnnotations = closestAnnotations.filter({
        $0.dynamicType === StationAnnotation.self
    })

无法转换类型(_)的值 – > Bool到期望参数类型(MKAnnotation) – >布尔

但接受:

let closestStationAnnotations = closestAnnotations.filter({
        (Annotation : MKAnnotation) -> Bool in
        Annotation.dynamicType === StationAnnotation.self
    })

解决方法

我一直在尝试不@R_670_11197@的代码(使用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)
})

如果您注意到错误消息,编译器会将闭包视为(_) – >布尔.

这使我得出结论,表达式$0.dynamicType以某种方式被优化出来.

最有趣的是

let closestStationAnnotations = closestAnnotations.filter({
    return true
})

会触发相同的错误.

所以我认为有两个编译器错误

>编译器无法从数组类型推断出参数,这是错误的,因为(_) – > Bool应被视为(类型) – > Bool在[Type]上调用时.>编译器以某种方式优化$0.dynamicType out,这显然是错误的.

大佬总结

以上是大佬教程为你收集整理的为什么swift编译器有时不接受速记参数名称?全部内容,希望文章能够帮你解决为什么swift编译器有时不接受速记参数名称?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。