[英]Accessing an Arrays and Dictionaries with Swift?
我正在尝试使用以下功能组装UIColor:
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)
}
}
尝试这种类型的组织:
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"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.