Swift Extension Not working
Asked Answered
S

2

9

I've added an extension to UIColor for some colors that I use throughout my app. Here's an example:

extension UIColor {
    func appLightGrayColor() -> UIColor {
    return UIColor(red: 190.0/255.0, green: 190.0/255.0, blue: 190.0/255.0, alpha: 1.0)
    }

    func grayScaleColor(grayScale : CGFloat) -> UIColor {
    return UIColor(red: grayScale/255.0, green: grayScale/255.0, blue: grayScale/255.0, alpha: 1.0)
    }
}

However, when I try to call it, the only way that I've been able to compile without errors is this:

UINavigationBar.appearance().barTintColor = UIColor.appLightGrayColor(UIColor())()

Here's what I get with autocomplete:

enter image description here

What am I doing wrong?

Santo answered 23/6, 2014 at 22:53 Comment(0)
S
2

While Bryan's answer is still correct, with the release of Swift 3, the preferred "Swifty" way of doing things has changed a bit.

With Swift 3, predefined UIColors are used accordingly:

var myColor: UIColor = .white // or .clear or whatever

Therefore, if you want something similar, such as the following...

var myColor: UIColor = .myCustomColor

...then, you would define the extension like so:

extension UIColor
    {
    public class var myCustomColor: UIColor
        {
        return UIColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1.0)
        }
    }

In fact, Apple defines white as:

public class var white: UIColor
Stavros answered 8/8, 2016 at 23:38 Comment(3)
Talking about the "Swifty" way of doing things... Shouldn't opening braces be on the same line as the statement? :)Lucerne
Don't get me started on that! It will only become a religious war. :-)Stavros
haha I know I know - everyone has their way :P I follow something similar to github.com/raywenderlich/swift-style-guideLucerne
A
19

You have added instance method, but what you really want is class method

extension UIColor {
    class func appLightGrayColor() -> UIColor {
    return UIColor(red: 190.0/255.0, green: 190.0/255.0, blue: 190.0/255.0, alpha: 1.0)
    }

    class func grayScaleColor(grayScale : CGFloat) -> UIColor {
    return UIColor(red: grayScale/255.0, green: grayScale/255.0, blue: grayScale/255.0, alpha: 1.0)
    }
}
Arabeila answered 23/6, 2014 at 22:58 Comment(1)
+1, also, I suggest that appLightGrayColor() -> UIColor { return UIColor.grayScaleColor(190.0) }Acinus
S
2

While Bryan's answer is still correct, with the release of Swift 3, the preferred "Swifty" way of doing things has changed a bit.

With Swift 3, predefined UIColors are used accordingly:

var myColor: UIColor = .white // or .clear or whatever

Therefore, if you want something similar, such as the following...

var myColor: UIColor = .myCustomColor

...then, you would define the extension like so:

extension UIColor
    {
    public class var myCustomColor: UIColor
        {
        return UIColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1.0)
        }
    }

In fact, Apple defines white as:

public class var white: UIColor
Stavros answered 8/8, 2016 at 23:38 Comment(3)
Talking about the "Swifty" way of doing things... Shouldn't opening braces be on the same line as the statement? :)Lucerne
Don't get me started on that! It will only become a religious war. :-)Stavros
haha I know I know - everyone has their way :P I follow something similar to github.com/raywenderlich/swift-style-guideLucerne

© 2022 - 2024 — McMap. All rights reserved.