2017-02-03 23 views
0

我正在製作一個遊戲來學習Swift並試圖使代碼更乾淨更好。 我製作的一個叫做Utilities類:工具類與陣列

class Utilities: NSObject { 

    //Red, Green, Blue, Yellow, Purple 
    public let mColors = ["#DA4167", "#81E979","#2B3A67", "#FFFD82", "#3D315B"] 

    class func hexStringToUIColor (hex:String) -> UIColor { 
     var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() 

     if (cString.hasPrefix("#")) { 
      cString.remove(at: cString.startIndex) 
     } 

     if ((cString.characters.count) != 6) { 
      return UIColor.gray 
     } 

     var rgbValue:UInt32 = 0 
     Scanner(string: cString).scanHexInt32(&rgbValue) 

     return UIColor(
      red: CGFloat((rgbValue & 0xFF0000) >> 16)/255.0, 
      green: CGFloat((rgbValue & 0x00FF00) >> 8)/255.0, 
      blue: CGFloat(rgbValue & 0x0000FF)/255.0, 
      alpha: CGFloat(1.0) 
     ) 
    } 
} 

如何使用mColors另一個類? 我有另一個類,在那裏我試圖用mColors,這是該行:

mRingOne.fillColor = Utilities.hexStringToUIColor(hex: mColors[0]) 

我得到這個錯誤:

Use of unresolved identifier 'mColors' 
+2

實用工具類幾乎都是一個錯誤的決定。 'hexStringToUIColor'可能會更好,因爲'UIColor'擴展 – Sulthan

+0

謝謝,我想我現在會堅持這個類,因爲我需要在那裏放更多的方法。 – swiftnewbie

+1

這不是Java。正如蘇丹和哈米什指出的那樣,斯威夫特讓我們擴展了其他民族的階級。我們儘可能使用這些方法,而不是對任何其他地方都不適合的整個混蛋兒方法進行任意實用類。 – Alexander

回答

1

由於@Sulthan says,你真的不應該使用這個工具類(更不用說從NSObject繼承非final實用工具類!)。

您應該將hexStringToUIColor轉換爲UIColor的擴展名,我建議您也創建一個便捷的初始化程序。如果你仍然想要一個命名空間,你可以使用一個沒有大小寫的enum(這比structclass更受歡迎,因爲它可以防止初始化)。

此外,我會建議不要使用數組來存儲您的十六進制顏色字符串(除非您實際需要迭代它們出於某種原因)。 mColors[0]不會說「紅色」,所以請使用實際名稱代替static屬性。他們也可能會更有用,因爲UIColor對象而不是String s。

這裏的這些建議的例子:

extension UIColor { 

    convenience init(hex: String) { 

     var hex = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() 

     if hex.hasPrefix("#") { 
      hex.remove(at: hex.startIndex) 
     } 

     guard hex.characters.count == 6 else { 
      self.init(cgColor: UIColor.gray.cgColor) 
      return 
     } 

     var rgbValue: UInt32 = 0 
     Scanner(string: hex).scanHexInt32(&rgbValue) 

     self.init(
      red: CGFloat((rgbValue & 0xFF0000) >> 16)/255, 
      green: CGFloat((rgbValue & 0x00FF00) >> 8)/255, 
      blue: CGFloat(rgbValue & 0x0000FF)/255, 
      alpha: 1 
     ) 
    } 
} 

enum MySpecialColors { 
    static let red = UIColor(hex: "#DA4167") 
    static let green = UIColor(hex: "#81E979") 
    static let blue = UIColor(hex: "#2B3A67") 
    static let yellow = UIColor(hex: "#FFFD82") 
    static let purple = UIColor(hex: "#3D315B") 
} 

現在,如果你想用你的顏色,你剛纔說的東西,如:

mRingOne.fillColor = MySpecialColors.red 
+0

我應該爲擴展程序打開哪個文件?我對Swift非常陌生,並試圖練習良好的編程。謝謝 – swiftnewbie

+0

@swiftnewbie你可以把它放在它自己的.swift文件中,或者把它放在你的項目中一個現有的.swift文件的頂部 - 它可以在整個項目中使用(從技術上來說就是模塊)。 – Hamish

+0

非常感謝。 – swiftnewbie

1

移動這一行:

public let mColors = ["#DA4167", "#81E979","#2B3A67", "#FFFD82", "#3D315B"] 

...到「最高級別」,即將其放置在以外的任何大括號(如類別聲明的當前現在位置)的之外。

//Red, Green, Blue, Yellow, Purple 
public let mColors = ["#DA4167", "#81E979","#2B3A67", "#FFFD82", "#3D315B"] 
class Utilities: NSObject { 

編輯現在,我很抱歉,我認爲這一點。你的Utility類實際上不是一個類 - 它只是一些常量和函數的命名空間。命名空間很好。這將是更好的,因此,有靜態的成員結構,如在其他答案建議:

struct Utilities { 
    static let mColors = ["#DA4167", "#81E979","#2B3A67", "#FFFD82", "#3D315B"] 
    // ... 
} 

從任何地方訪問的語法將被Utilities.mColors

+0

然後看我的書。花括號是_scope_:http://www.apeth.com/swiftBook/ch01.html#_scope_and_lifetime – matt

+0

謝謝,工作正常。 – swiftnewbie

+0

但現在我有「回答者的悔恨」。我修改了我的答案。另一個答案應該是接受的答案。 – matt

0

集mColors爲靜態:

static let mColors = ["#DA4167", "#81E979","#2B3A67", "#FFFD82", "#3D315B"] 

,並稱之爲:

mRingOne.fillColor = Utilities.hexStringToUIColor(hex: Utilities.mColors[0]) 

你可以做靜態讓每一個顏色:

static let myRed = "#DA4167" 
static let myGreen = "#81E979" 

,並調用它:

mRingOne.fillColor = Utilities.hexStringToUIColor(hex: Utilities.myRed) 
+0

非常感謝。儘管我第一次回答。 – swiftnewbie

+1

其實在許多方面這個答案比我的更好。你的工具類實際上不是一個類 - 它只是一些常量和函數的命名空間。這會更好的作爲一個靜態成員類(或更好,一個結構)。訪問的語法將是'Utilities.mColors'。 – matt

+0

謝謝你們兩位。 – swiftnewbie