0

I am trying to create a Type extension for UIColor per the code snippet below, but I'm receiving a build error. When I try to create the UIColor object in my Type extension method, the UIColor constructor is referencing the encapsulating UIColor extension I created. How to instantiate a UIColor object in my UIColor Type extension method?

 // 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
                    }
                }
            }
        }
gangelo
  • 3,034
  • 4
  • 29
  • 43
  • 2
    Delete `class UIColor { }` – you want `class var seventyPercentGreyColor : UIColor {...}` instead (you can also remove the explicit `get { }`) – Hamish Sep 12 '16 at 18:12

1 Answers1

1

You can just declare it as static. If you just need grey levels you can use 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
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571