I am still trying to grasp all these programming concepts and I have a simple UIView with a rgb color and I am using this on many different views.
Right now I am still hard coding the color in every view:
UIColor(red: 120/255, green: 51/255, blue: 2/255, alpha: 1.0
But I'd love to create a single swift file (called Colors.swift for instance) and there I'd like to put all colors.
Now in every single view I'd like to just get the color from this file, so that I have only one place to change the colors if necessary.
I know how to create a function in a swift file and call it from another place. For example all animations I reuse I place into one file, but with those colors I'm having a blackout. I have no idea how to place them into another file and get them out of there. Perfect would be some kind of switch case with different named colors (blue, green,...) and their corresponding rgb values so that I can just call the name of the color to get it. This would be a swift file I create:
import UIKit
let myColors = UIColor()
switch myColors {
case .red:
UIColor(red: 120/255, green: 51/255, blue: 2/255, alpha: 1.0)
case .gray:
UIColor(red: 120/255, green: 120/255, blue: 120/255, alpha: 1.0)
default:
UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0)
}
But when I write a switch statement in a swift file I cannot get this to work. The error I get is "statements are not allowed on top level" and each case gets the error "Results of UIColor initializer is unused", but I do not see a way to place it inside something?
Maybe the whole approach is wrong?