perteadi
perteadi

Reputation: 13

Array of dictionaries for String to UIColor

i'm trying to make an array of dictionaries to combine a string to a UIColor . so like this all my color ( a lots of colors) will be easy to manage. i would like to call like : magenta and then , under the hood , to have the UIColor in RGB

i have tried this but it throws an error:

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

Upvotes: 0

Views: 435

Answers (2)

vadian
vadian

Reputation: 285160

The syntax you're looking for is

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

Alternatively define a struct outside any class

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)
}

and call a color with

let color = Color.Red

Upvotes: 1

Alexander
Alexander

Reputation: 2297

Try:

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

If you need change dictionary dynamically, use this code:

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

Upvotes: 0

Related Questions