Reputation: 117
struct ColorPalette {
var DefaultBarTintColor : UIColor = UIColorFromRGB(0x75b5d4)
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}
error : Cannot convert value of type "int' to expected argument type 'MyClassName'
If I do
view.backColor = UIColorFromRGB(UIColorFromRGB)
it works
But I want to do like this:
view.backColor = ColorPalette.DefaultBarTintColor
I don't know how to fix it
Upvotes: 1
Views: 2348
Reputation: 629
Instead of initializing property while declaration, you can have initializer function as below:
struct ColorPalette {
var DefaultBarTintColor : UIColor!
func UIColorFromRGB(rgbValue: Int) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
init(rgbValue: Int) {
self.DefaultBarTintColor = UIColorFromRGB(rgbValue)
}
}
And then create instance by passing hex value as below:
var color = ColorPalette(rgbValue: 0x75B5D4)
Upvotes: 0
Reputation: 9540
To call function inside struct
, you need to use static
keyword. Here is the modified function of your's!
struct ColorPalette {
static var DefaultBarTintColor : UIColor = ColorPalette.UIColorFromRGB(0x75b5d4)
static func UIColorFromRGB(rgbValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}
Now you can call like this!
view.backColor = ColorPalette.DefaultBarTintColor
Although, if you need to call without using the static
keyword then you need to make the object of your struct
. Like:
let myColorPallete = ColorPalette()
then you can access it like this:
viewNoEvents.backgroundColor = myColorPallete.DefaultBarTintColor
Hope this helps!
Upvotes: 2