为什么swift编译器有时不接受速记参数名称?
发布时间:2020-12-14 04:55:46 所属栏目:百科 来源:网络整理
导读:数组是:var nearestAnnotations:[MKAnnotation] 我想知道swift编译器为什么不接受: let closestStationAnnotations = closestAnnotations.filter({ $0.dynamicType === StationAnnotation.self }) 无法转换类型(_)的值 – Bool到期望参数类型(MKAnnotatio
数组是: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 }) 解决方法
我一直在尝试不同版本的代码(使用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,这显然是错误的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |