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

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

概述在最近的一个问题中,海报中有这个有趣的代码: self.view.backgroundColor = .whiteColor() 我很惊讶地看到这一点.我只看过用于枚举值的领先点符号.在这种情况下,backgroundColor类型为UIColor?而whiteColor是返回UIColor的UIColor上的一个类方法. 为什么这个工作?这是一种合法的方法来调用工厂方法? 此功能称为“ Impl 在最近的一个问题中,海报中有这个有趣的代码:
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 variablelet col: UIcolor?col = .redcolor()// pass to functionfunc 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?

总结

以上是内存溢出为你收集整理的称为Swift类工厂方法与领先的点符号?全部内容,希望文章能够帮你解决称为Swift类工厂方法与领先的点符号?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1046344.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存