2016-09-12 128 views
0

我想爲下面的代碼片段中的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 
        } 
       } 
      } 
     } 
+2

刪除'類的UIColor {}' - 你想'類VAR seventyPercentGreyColor:的UIColor {...}',而不是(你可以也刪除顯式的'get {}') – Hamish

回答

1

您可以將其聲明爲靜態。如果你只需要灰度級可以使用的UIColor(白:阿爾法:)初始化:

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

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