@objc public protocol Sound {
@objc optional func woo()
}
public class Foo: NSObject, Sound {
func action() {
((self as Sound).woo ?? default_woo)() // calls `woo` method if available or defaults to default_woo method
This file contains hidden or 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
| // Source: http://stackoverflow.com/a/40634752/3400034 | |
| extension UIAlertController { | |
| func tapButton(at index: Int, animated: Bool) { | |
| guard Thread.isMainThread else { | |
| DispatchQueue.main.async { | |
| self.tapButton(at: index, animated: animated) | |
| } | |
| return | |
| } | |
This file contains hidden or 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
| enum Planets: String { | |
| case Mercury | |
| case Venus | |
| case Earth | |
| } | |
| // Add protocol to enums that need it | |
| public protocol UsesRawValue { | |
| var rawValue: String { get } | |
| } |
This file contains hidden or 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
| // An enum of some planets | |
| enum Planets: String { | |
| case Mercury | |
| case Venus | |
| case Earth | |
| } | |
| // This extension will allow us to use string enums without | |
| // explicitly calling rawValue to get the string | |
| extension Dictionary where Key: ExpressibleByStringLiteral { |
This file contains hidden or 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
| my_viewcontroller.customizerObject = SpecialRed() | |
| my_viewController.customizerObject = SpecialBlue() |
This file contains hidden or 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
| // SWIFT | |
| class SpecialRed: SpecialProtocol<ViewController> { | |
| override func specialMethod(_ viewController: ViewController) { | |
| // customization | |
| } | |
| } | |
| // Objective-C | |
| @interface SpecialBlue: SpecialProtocol<ViewController *> | |
| @end |
This file contains hidden or 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 ViewController: UIViewController { | |
| var customizerObject: SpecialProtocol<ViewController>? | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| specialMethod() | |
| } |
This file contains hidden or 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
| @interface SpecialProtocol < | |
| __covariant VIEW_CONTROLLER: UIViewController * | |
| > : NSObject | |
| - (void)specialMethod:(VIEW_CONTROLLER _Nonnull)viewController; | |
| @end |
This file contains hidden or 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
| protocol SpecialProtocol: class { | |
| func specialMethod(_ vc: ViewController) | |
| } | |
| class VCCustomizer: NSObject, SpecialProtocol { | |
| func specialMethod(_ vc: ViewController) { | |
| // Special implementation | |
| } | |
| } |