在最近的一个问题中,海报中有这个有趣的代码:
self.view.backgroundColor = .whiteColor()
我很惊讶地看到这一点.我只看过用于枚举值的领先点符号.在这种情况下,backgroundColor类型为UIColor?而whiteColor是返回UIColor的UIColor上的一个类方法.
为什么这个工作?这是一种合法的方法来调用工厂方法?
此功能称为“
Implicit Member Expression”
An implicit member expression is an abbreviated way to access a member of a type,such as an enumeration case or a class method,in a context where type inference can determine the implied type. It has the following form:
.
member name但是,截至目前,我建议您不要在“可选”或“不可忽略的可选”上下文中使用此功能.
虽然这样做:
// store in Optional variable let col: UIColor? col = .redColor() // pass to function func f(arg:UIColor?) { println(arg) } f(.redColor())这会使编译器崩溃:(
func f(arg:UIColor?,arg2:Int) { println(arg) } // ^^^^^^^^^^ just added this. f(.redColor(),1)编译器有一些错误.见:does swift not allow initialization in function parameters?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。