如何在类型扩展方法中实例化类型?

How to instantiate a Type in a Type extension method?

我正在尝试根据下面的代码片段为 UIColor 创建类型扩展,但我收到构建错误。当我尝试在我的类型扩展方法中创建 UIColor 对象时,UIColor 构造函数引用了我创建的封装 UIColor 扩展。如何在我的 UIColor 类型扩展方法中实例化 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
                    }
                }
            }
        }

您可以将其声明为静态的。如果你只需要灰度级,你可以使用 UIColor(white:alpha:) initializer:

extension UIColor {
    static var seventyPercentBlack: UIColor { return UIColor(white: 0.3, alpha: 1) }
}

UIColor.seventyPercentBlack   // w 0,3 a 1,0