当前位置: 动力学知识库 > 问答 > 编程问答 >

Accessing an Arrays and Dictionaries with Swift?

问题描述:

I am trying assemble a UIColor using the following function:

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)

}

}

I am drawing he color makeup from the following library (using the scale key):

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]

]

]

]

}

I am wondering if the function is taking the incorrect parameter or if I have to modify my code to fit the function. I have tried to modify the parameter but I get a ton of errors. So I guess my question is the following:

How would I create a function to accept the scale keys color values or, if that will not work, do I have to change my code overall or the library itself?

Here is all my code:

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)

}

}

Other file:

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]

]

]

]

}

Any suggestions or input is greatly appreciated. Please leave the updated, delete, added, or substituted code in your answer and explain why you did what you did.

Thanks in advance.

EDIT

How would I modify the following code to fit the changes?

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)

}

}

网友答案:

Try this type of organization:

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"]
分享给朋友:
您可能感兴趣的文章:
随机阅读: