<分区>
在 Swift 中调用方法而不初始化类的一个完美示例是 UIColor
。
您可以执行 UIColor.whiteColor()
。请注意 ()
如何位于 whiteColor
的末尾。
如果我要为自定义方法扩展 UIColor
:
extension UIColor {
/** Set the color to white. */
func white() -> UIColor {
return UIColor.whiteColor()
}
}
我必须像这样调用此方法:UIColor().white()
而不是这样的 UIColor.white()
。
如何编写初始化程序仅在末尾的方法?还是仅当类是用 Objective-C 编写时才可用?