Created
May 29, 2015 16:27
-
-
Save JanGorman/013ebb78377cf3400a95 to your computer and use it in GitHub Desktop.
Smart Swift Initialisation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo { | |
// Keep default values for optionals anyway since we can do shorter self.init calls | |
// when the call is unambiguous from the convenience init | |
private init(image: UIImage? = nil, images: [UIImage]? = nil, imageURL: NSURL? = nil, blurStyle: UIBlurEffectStyle = .Dark) { | |
// init | |
} | |
// By exposing convenience initialisers that take mutually exclusive parameters | |
// we remove the need to do any kind of assertion on them | |
public convenience init(image: UIImage, blurStyle: UIBlurEffectStyle = .Dark) { | |
self.init(image: image, images: nil, blurStyle: blurStyle) | |
} | |
public convenience init(images: [UIImage], blurStyle: UIBlurEffectStyle = .Dark) { | |
self.init(image: nil, images: images, blurStyle: blurStyle) | |
} | |
public convenience init(imageURL: NSURL, blurStyle: UIBlurEffectStyle = .Dark) { | |
// No need to set images to nil here since this call is unambiguous | |
self.init(image: nil, imageURL: imageURL, blurStyle: blurStyle) | |
} | |
} | |
let foo1 = Foo(UIImage(named: "fun")!) | |
let foo2 = Foo([UIImage(…)!, UIImage(…)!], blurStyle: .Light) | |
let foo3 = Foo(NSURL(string: …)!) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment