称为Swift类工厂方法与领先的点符号?

在最近的一个问题中,海报中有这个有趣的代码
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?

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...