我正在尝试根据下面的代码片段为UIColor创建一个Type扩展,但是我收到了一个构建错误。当我尝试在我的Type扩展方法中创建UIColor对象时,UIColor构造函数引用我创建的封装UIColor扩展。如何在我的UIColor Type扩展方法中实例化UIColor对象?
// Error: "Argument to call takes no parameters"
import UIKit
import Foundation
extension UIColor {
class UIColor {
var seventyPercentGreyColor : UIColor {
get {
let seventyPercent:CGFloat = (1.0 - 0.70)
// The below line of code produces a
// "Argument to call takes no parameters" build error
let color = UIColor(red: seventyPercent, green: seventyPercent, blue: seventyPercent, alpha:1.0)
return color
}
}
}
}
答案 0 :(得分:1)
您可以将其声明为静态。如果你只需要灰度级,你可以使用UIColor(白色:alpha :)初始化器:
extension UIColor {
static var seventyPercentBlack: UIColor { return UIColor(white: 0.3, alpha: 1) }
}
UIColor.seventyPercentBlack // w 0,3 a 1,0