2016-02-01 62 views
-1

我嘗試使用下面的函數組裝的UIColor:使用Swift訪問數組和字典?

var backgroundColor: UIColor = UIColor.clearColor()  

let colorsDictionary = chemistryDictionary["backgroundColor"] as! [String: CGFloat] 

    backgroundColor = rgbColorFromDictionary(colorsDictionary) 
} 

func rgbColorFromDictionary(colorDictionary: [String: CGFloat]) -> UIColor { 
    let red = colorDictionary["red"]! 
    let green = colorDictionary["green"]! 
    let blue = colorDictionary["blue"]! 
    let alpha = colorDictionary["alpha"]! 

    return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha) 
} 
} 

我畫從以下庫他彩妝(使用縮放鍵):

struct ColorMatchLibrary { 
let library = [ 

// pH Array 
[ "title": "pH", 
"description": "Most living things depend on proper pH level to sustain life.", 
"scale": 
["6.2": 
["red": 212, "green": 142, "blue": 69, "alpha": 1.0], 

"6.8": 
["red": 209, "green": 122, "blue": 31, "alpha": 1.0], 

"7.2": 
["red": 196, "green": 80, "blue": 9, "alpha": 1.0], 

"7.8": 
["red": 194, "green": 74, "blue": 58, "alpha": 1.0], 

"8.4": 
["red": 208, "green": 48, "blue": 75, "alpha": 1.0] 
] 
], 
// Ammonia Array 

[ "title": "Ammonia", 
"description": "", 
"scale": 
[ 
"0": 
["red": 244, "green": 235, "blue": 130, "alpha": 1.0], 
".25": 
["red": 233, "green": 233, "blue": 156, "alpha": 1.0], 
".5": 
["red": 223, "green": 238, "blue": 141, "alpha": 1.0], 
"1.0": 
["red": 221, "green": 236, "blue": 210, "alpha": 1.0], 
"3.0": 
["red": 202, "green": 227, "blue": 191, "alpha": 1.0], 
"6.0": 
["red": 186, "green": 216, "blue": 173, "alpha": 1.0] 
] 
] 


] 
} 

我想知道如果函數正在採取不正確的參數,或者如果我不得不修改我的代碼以適合該功能。我試圖修改參數,但我得到了很多錯誤。所以我想我的問題如下:

我將如何創建一個函數來接受縮放鍵顏色值,或者,如果這不起作用,我必須改變我的代碼整體或庫本身?

這裏是我的所有代碼:

var backgroundColor: UIColor = UIColor.clearColor()  

let colorsDictionary = chemistryDictionary["backgroundColor"] as! [String: CGFloat] 

    backgroundColor = rgbColorFromDictionary(colorsDictionary) 
} 

func rgbColorFromDictionary(colorDictionary: [String: CGFloat]) -> UIColor { 
    let red = colorDictionary["red"]! 
    let green = colorDictionary["green"]! 
    let blue = colorDictionary["blue"]! 
    let alpha = colorDictionary["alpha"]! 

    return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha) 
} 
} 

其他文件:

struct ColorMatchLibrary { 
let library = [ 

// pH Array 
[ "title": "pH", 
"description": "Most living things depend on proper pH level to sustain life.", 
"scale": 
["6.2": 
["red": 212, "green": 142, "blue": 69, "alpha": 1.0], 

"6.8": 
["red": 209, "green": 122, "blue": 31, "alpha": 1.0], 

"7.2": 
["red": 196, "green": 80, "blue": 9, "alpha": 1.0], 

"7.8": 
["red": 194, "green": 74, "blue": 58, "alpha": 1.0], 

"8.4": 
["red": 208, "green": 48, "blue": 75, "alpha": 1.0] 
] 
], 
// Ammonia Array 

[ "title": "Ammonia", 
"description": "", 
"scale": 
[ 
"0": 
["red": 244, "green": 235, "blue": 130, "alpha": 1.0], 
".25": 
["red": 233, "green": 233, "blue": 156, "alpha": 1.0], 
".5": 
["red": 223, "green": 238, "blue": 141, "alpha": 1.0], 
"1.0": 
["red": 221, "green": 236, "blue": 210, "alpha": 1.0], 
"3.0": 
["red": 202, "green": 227, "blue": 191, "alpha": 1.0], 
"6.0": 
["red": 186, "green": 216, "blue": 173, "alpha": 1.0] 
] 
] 
] 
} 

任何建議或輸入是極大的讚賞。請在回答中留下更新,刪除,添加或替換的代碼,並解釋爲什麼你做了你所做的事情。

在此先感謝。

編輯

我將如何修改下面的代碼,以適應變化?

struct Chemistry { 

var title: String? 
var description: String? 
var backgroundColor: UIColor = UIColor.clearColor() 

init(index: Int) { 
    let colorMatchLibrary = ColorMatchLibrary.library 
    let chemistryDictionary = colorMatchLibrary[index] 

    title = chemistryDictionary["title"] as! String! 
    description = chemistryDictionary["description"] as! String! 

    let colorsDictionary = chemistryDictionary["backgroundColor"] as! [String: UIColor] 

    backgroundColor = rgbColorFromDictionary(colorsDictionary) 
} 

func rgbColorFromDictionary(colorDictionary: [String: UIColor]) -> UIColor { 
    let red = colorDictionary["red"]! 
    let green = colorDictionary["green"]! 
    let blue = colorDictionary["blue"]! 
    let alpha = colorDictionary["alpha"]! 

    return UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: alpha) 
} 
} 
+0

爲什麼你存儲你'ColorMatchLibrary'這樣?爲什麼不把它分解成更容易訪問的小結構?另外,爲什麼不直接存儲'UIColor'對象而不是存儲組件值並在每次使用時創建它們? – ColGraff

+0

我明白你的意思是分解圖書館,但是通過直接存儲UIColors你是什麼意思?另外,如果我有2個不同的顏色部分,我可以將它們存儲在同一個結構中,還是應該將它們分開。如果我吐了他們,那我該如何編碼函數呢? – Bigfoot11

+0

而不是存儲每個比例尺的顏色值字典,您可以簡單地創建一個'[String:UIColor]'的字典,然後當您查看比例尺時,您將直接獲得顏色。 – ColGraff

回答

2

嘗試這種類型的組織:

struct ColorMatchInfo { 
    let title:String 
    let description:String 
    let scaleColors:[String:UIColor] 
} 

extension UIColor { 
    convenience init(red:Int, green:Int, blue:Int) { 
    self.init(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: 1.0) 
    } 
} 

let colorMatches = [ 
    ColorMatchInfo(title: "ph", 
       description: "Most living things depend on proper pH level to sustain life.", 
       scaleColors: [ 
        "6.2": UIColor(red: 212, green: 142, blue: 69), 
        "6.8": UIColor(red: 209, green: 122, blue: 31), 
        "7.2": UIColor(red: 196, green: 80, blue: 9), 
        "7.8": UIColor(red: 194, green: 74, blue: 58), 
        "8.4": UIColor(red: 208, green: 48, blue: 75) 
    ]), 
    ColorMatchInfo(title: "Ammonia", 
       description: "", 
       scaleColors: [ 
        "0.00": UIColor(red: 244, green: 235, blue: 130), 
        "0.25": UIColor(red: 233, green: 233, blue: 156), 
        "0.50": UIColor(red: 223, green: 238, blue: 141), 
        "1.00": UIColor(red: 221, green: 236, blue: 210), 
        "3.00": UIColor(red: 202, green: 227, blue: 191), 
        "6.00": UIColor(red: 202, green: 216, blue: 173) 
    ]), 
] 

let aColor = colorMatches[0].scaleColors["6.2"] 
+0

我修改了我的問題。我會接受你的答案,但我將如何修改編輯代碼以適應上面的代碼? – Bigfoot11

+0

@Buster你的任何字典都沒有關鍵字「backgroundDictionary」。它會是你的'ColorMatchLibrary'的每個索引下的結構的一部分嗎? – ColGraff

+0

背景顏色是我擁有的每個UIButton的背景顏色(6)。我不知道我現在是否需要這個,現在我正在使用[String:UIColor]。但是,我將如何控制哪個按鈕是哪種顏色? – Bigfoot11