Within the Javascript or jQuery it is possible to concat strings and variable names like:
var id = 5;
$("#wrapper_" + id).hide();
With Swift I want to achieve the same, something like:
func eventColors (colorID: Int, view: UIView) {
view.backgroundColor = Constants.Color.eventColor + colorID // Incorrect
}
Constants structure:
struct Constants {
struct Color {
static let eventColor1: UIColor = UIColor(red:0.06, green:0.44, blue:0.64, alpha:1)
static let eventColor2: UIColor = UIColor(red:0.86, green:0.30, blue:0.99, alpha:1)
static let eventColor3: UIColor = UIColor(red:0.50, green:0.44, blue:0.64, alpha:1)
}
}
You cannot create variable names on the fly in Swift. You should consider using an array here instead. Make eventColor
an array of colors:
struct Constants {
struct Color {
static let eventColor: [UIColor] = [
UIColor(red:0.06, green:0.44, blue:0.64, alpha:1),
UIColor(red:0.86, green:0.30, blue:0.99, alpha:1),
UIColor(red:0.50, green:0.44, blue:0.64, alpha:1)
]
}
}
And then select your color like this:
view.backgroundColor = Constants.Color.eventColor[colorID - 1]