Optional protocol methods in Swift

In Swift it’s kind of unsupported to implemented optional protocol methods. You can, which is ugly, use the @objc syntax: @objc protocol MyProtocol { optional func doSomething(); } class MyClass : MyProtocol { // no error } Another disadvantage here is that structs are unsupported, as you’re bridging for Objc. Using protocol extensions to create … 

 

Protocol extensions and typed arrays

Swift 2.0 introduces protocol extensions, aswel as typed array extensions. This makes it possible to extend an array containing a specific type or even add methods to class inheriting a specific protocol. Typed array protocol extensions With typed array extensions you can create methods for Arrays containing a specific type only. For example, if you … 

 

NS_OPTIONS in Swift

Swift 2.0 introduced OptionSetType which makes it possible to define a NS_OPTIONS like way of options. NS_OPTIONS types get imported as conforming to the OptionSetType protocol, which presents a set-like interface for options: struct CoffeeManipulators : OptionSetType { let rawValue: Int static let Milk = CoffeeManipulators(rawValue: 1) static let Sugar = CoffeeManipulators(rawValue: 2) static let … 

 
Follow on Feedly