Uicolor Extension in Swift Error

Error while accessing members of UIColor extension

The compiler cannot know that you are referring to a member of
UIColor. Either

private static let defaultBorderColor = UIColor.nonSelectedTabColor

or

private static let defaultBorderColor: UIColor = .nonSelectedTabColor

would solve the issue. In the second line, the type UIColor
is inferred from the context, and .nonSelectedTabColor
is an “implicit member expression.”

How to access extension of UIColor in Swift?

You have defined an instance method, which means that you can call
it only on an UIColor instance:

let col = UIColor().getCustomBlueColor()
// or in your case:
btnShare.setTitleColor(UIColor().getCustomBlueColor(), forState: .Normal)

The compiler error "missing argument" occurs because
Instance Methods are Curried Functions in Swift,
so it could equivalently be called as

let col = UIColor.getCustomBlueColor(UIColor())()

(But that would be a strange thing to do, and I have added it only to
explain where the error message comes from.)


But what you really want is a type method (class func)

extension UIColor{
class func getCustomBlueColor() -> UIColor{
return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
}
}

which is called as

let col = UIColor.getCustomBlueColor()
// or in your case:
btnShare.setTitleColor(UIColor.getCustomBlueColor(), forState: .Normal)

without the need to create an UIColor instance first.

Overriding or extending UIColor to support certain protocols

I checked your situation, and I have also the impression that the compiler gets confused.

But there might be a solution. The following code compiles for me without problems:

public protocol MyProtocol {
init(myValue: Any) throws
}

class MyColor:UIColor {
convenience init(myValue: Any) throws {
self.init(red: 0, green: 0, blue: 0, alpha: 1)
}
}

EDIT:

I am sorry: Although this code compiles, it is missing the protocol.

Here is (hopefully) the correct code:

class MyColor:UIColor, MyProtocol {
required convenience init(myValue: Any) throws {
self.init(red: 0, green: 0, blue: 0, alpha: 1)
}
}

UIColor extension

In you method, Change "self" to "[UIColor self]".

+ (UIColor *)directOrLegacyWithColor {
UIColor *color;
UIColor *returenedColor = self.changeColor;
color = LoginsManager.getSharedInstance.isDirect ? returenedColor : [UIColor self];
return color;
}

Declaration is on valid at file scope (UI color extension)

Is a previous block perhaps missing a close curly brace? If a preceding class or extension is still open, then this extension isn't at file scope – it's one level deeper, which is invalid. I'd double-check that all of your preceding scopes are closed as you expect.

UIColor extension convenience init not working

I don't see any inconvenience doing it just like this:

let myCustomColorHSBa = UIColor(hue: 120/360, saturation: 0.25 , brightness: 1.0 , alpha: 1)
let myCustomColorRGBa = UIColor(red: 191/255, green: 1, blue: 191/255, alpha: 1)

but if you really need one, you can do as follow:

extension UIColor {
convenience init(red: Int = 0, green: Int = 0, blue: Int = 0, opacity: Int = 255) {
precondition(0...255 ~= red &&
0...255 ~= green &&
0...255 ~= blue &&
0...255 ~= opacity, "input range is out of range 0...255")
self.init(red: CGFloat(red)/255, green: CGFloat(green)/255, blue: CGFloat(blue)/255, alpha: CGFloat(opacity)/255)
}
}

UIColor(red: 255)               // r 1.0 g 0.0 b 0.0 a 1.0  (Red)
UIColor(red: 255, green: 255) // r 1.0 g 1.0 b 0.0 a 1.0 (Yellow)
UIColor(red: 255, blue: 255) // r 1.0 g 0.0 b 1.0 a 1.0 (Magenta)

UIColor(green: 255) // r 0.0 g 1.0 b 0.0 a 1.0 (Green)
UIColor(green: 255, blue: 255) // r 0.0 g 1.0 b 1.0 a 1.0 (Cyan)

UIColor(blue: 255) // r 0.0 g 0.0 b 1.0 a 1.0 (Blue)
UIColor(red: 255, green: 192, blue: 203) // r 1.0 g 0.753 b 0.796 a 1.0 (Pink)
UIColor(red: 255, green: 215) // r 1.0 g 0.843 b 0.0 a 1.0 (Gold)


Related Topics



Leave a reply



Submit