String到UIColor的字典数组

时间:2015-10-24 17:35:32

标签: ios arrays swift dictionary uicolor

我正在尝试制作一个字典数组,以便将字符串组合到UIColor中。所以我的所有颜色(很多颜色)都很容易管理。我想称之为:magenta,然后在引擎盖下,将RGB中的UIColor

我试过这个,但它引发了一个错误:

let gColor: [String]:[UIColor] = 
        ["magenta"]:[UIColor(red: 0, green: 0, blue: 0, alpha: 1)]
        // and like 100 colors are stored here , but isn't working when i call them

2 个答案:

答案 0 :(得分:1)

您正在寻找的语法是

var gColor: [String : UIColor] = ["magenta":UIColor(red: 0, green: 0, blue: 0, alpha: 1)]

或者在任何类

之外定义结构
struct Color {
 static let Black = UIColor(red: 0, green: 0, blue: 0, alpha: 1)
 static let Red = UIColor(red: 1.0, green: 0, blue: 0, alpha: 1)
}

并使用

调用颜色
let color = Color.Red

答案 1 :(得分:0)

尝试:

let gColor: [String:UIColor] = ["magenta": UIColor(red: 0, green: 0, blue: 0, alpha: 1)] 

如果您需要动态更改字典,请使用以下代码:

var gColor = [String:UIColor]()
gColor["magenta"] = UIColor(red: 0, green: 0, blue: 0, alpha: 1)