加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:42:30 所属栏目:百科 来源:网络整理
导读:在最近的一个问题中,海报中有这个有趣的代码: self.view.backgroundColor = .whiteColor() 我很惊讶地看到这一点.我只看过用于枚举值的领先点符号.在这种情况下,backgroundColor类型为UIColor?而whiteColor是返回UIColor的UIColor上的一个类方法. 为什么这
在最近的一个问题中,海报中有这个有趣的代码:
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?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读