0

I am trying to create custom properties in Swift that allow me to omit the base class. Something like what is allowed with the base colors within UIColor.

self.backgroundColor = .blueColor() 

I know that works but I want to add my own custom colors. I would like the syntax to be something like:

self.backgroundColor = .customColor()
JAL
  • 41,701
  • 23
  • 172
  • 300
Cody Weaver
  • 4,756
  • 11
  • 33
  • 51

2 Answers2

3

You can simply write an extension for UIColor in order to do this. For example:

extension UIColor {

    // your custom color function
    class func customColor() -> UIColor {
        return UIColor(red: 0.3, green: 0.4, blue: 0.5, alpha: 0.6)
    }
}


...

self.backgroundColor = .customColor()

Although I had no idea that convenience syntax was in the language... thought it was only for enums. So thanks for showing me that!

Hamish
  • 78,605
  • 19
  • 187
  • 280
  • 2
    The syntax exists whenever the compiler can infer the return type of the function. – JAL May 03 '16 at 20:46
  • I just needed to add the `class` keyword before func. I was doing an extension just func customColor() -> UIColor and it wouldn't work. – Cody Weaver May 03 '16 at 20:47
  • Talk about being burned, 20 seconds too late on my answer... Have my +1. – JAL May 03 '16 at 20:47
  • @JAL I gave you a sympathy upvote ;) Interesting about the dot syntax, the compiler seems to get confused for `backgroundColor` as it's an optional... it just gives me `.None` or `.Some` for the auto-complete. I guess I'm going to have to take a further look at that. – Hamish May 03 '16 at 20:48
  • Seems to be working fine for me, just no autocomplete support as there are too many possible options. – JAL May 03 '16 at 20:49
  • 1
    @CodyWeaver Yes, that wouldn't work as an instance method as you'd need to have a `UIColor` instance in the first place in order to use it, which would make it kind of useless. A `class` function means it's available at class level (i.e `UIColor.something()`) – Hamish May 03 '16 at 20:53
2

Are you asking for an extension on UIColor?

extension UIColor {
    static func customColor() -> UIColor {
        return self.grayColor().colorWithAlphaComponent(0.6)
    }
}

class MyClass {
    var backgroundColor: UIColor?
}

let instance = MyClass()
instance.backgroundColor = .customColor()
JAL
  • 41,701
  • 23
  • 172
  • 300
  • With this approach you can't use custom initializers though can you? I tried it using 'hex' codes and it didn't work. I just needed to add the class keyword in front of func within my extension. – Cody Weaver May 03 '16 at 20:51
  • 1
    @CodyWeaver Sure you can create your own `UIColor` convenience initialisers, see http://stackoverflow.com/questions/24263007/how-to-use-hex-colour-values-in-swift-ios – Hamish May 03 '16 at 20:55
  • Yeah, I use those initializers but then it doesn't allow me to use them within the functions. Compiler says must use 'required' initializer. – Cody Weaver May 03 '16 at 20:58
  • @CodyWeaver Working fine for me... make sure you're not creating any convenience initialisers that have the same signature as already existing UIColor initialisers. – Hamish May 03 '16 at 21:05
  • I must suck at coding or something because it doesn't work for me. I can do blueColor, blackColor, grayColor but I can't use the hex initializer if I do static func. I am happy using your solution. – Cody Weaver May 03 '16 at 21:10