在最近的一个问题中,海报中有这个有趣的代码:
self.view.backgroundColor = .whiteColor()
我很惊讶地看到这一点.我只看过用于枚举值的领先点符号.在这种情况下,backgroundColor类型为UIColor?而whiteColor是返回UIColor的UIColor上的一个类方法.
此功能称为“
Implicit Member Expression”
原文链接:https://www.f2er.com/swift/319871.htmlAn 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?