Creating an extension to filter nils from an Array in Swift

As of Swift 2.0, you don’t need to write your own extension to filter nil values from an Array, you can use flatMap, which flattens the Array and filters nils: let optionals : [String?] = [“a”, “b”, nil, “d”] let nonOptionals = optionals.flatMap{$0} print(nonOptionals) Prints: [a, b, d] Note: There are 2 flatMap functions: One … Read more

How does one declare optional methods in a Swift protocol?

1. Using default implementations (preferred). protocol MyProtocol { func doSomething() } extension MyProtocol { func doSomething() { /* return a default value or just leave empty */ } } struct MyStruct: MyProtocol { /* no compile error */ } Advantages No Objective-C runtime is involved (well, no explicitly at least). This means you can conform … Read more

Extend array types using where clause in Swift

If you want to extend only array with specific type. You should extend _ArrayType protocol. extension _ArrayType where Generator.Element == Int { func doSomething() { … } } If you extend Array you can only make sure your element is conformed some protocol else. i.e: extension Array where Element: Equatable { func doSomething() { … … Read more

How to define optional methods in Swift protocol?

1. Using default implementations (preferred). protocol MyProtocol { func doSomething() } extension MyProtocol { func doSomething() { /* return a default value or just leave empty */ } } struct MyStruct: MyProtocol { /* no compile error */ } Advantages No Objective-C runtime is involved (well, no explicitly at least). This means you can conform … Read more

Swift 3.0: compiler error when calling global func min(T,T) in Array or Dictionary extension

I see no reason why the compiler shouldn’t be able to resolve this function call, therefore I would consider it a bug (it has already been filed – see SR-2450). It seems to occur whenever attempting to call a top-level function with the same name, but unambiguously different signature to a method or property that’s … Read more

Return instancetype in Swift

Similar as in Using ‘self’ in class extension functions in Swift, you can define a generic helper method which infers the type of self from the calling context: extension UIViewController { class func instantiateFromStoryboard(storyboardName: String, storyboardId: String) -> Self { return instantiateFromStoryboardHelper(storyboardName, storyboardId: storyboardId) } private class func instantiateFromStoryboardHelper<T>(storyboardName: String, storyboardId: String) -> T { … Read more

Overriding methods in Swift extensions

Extensions cannot/should not override. It is not possible to override functionality (like properties or methods) in extensions as documented in Apple’s Swift Guide. Extensions can add new functionality to a type, but they cannot override existing functionality. Swift Developer Guide The compiler is allowing you to override in the extension for compatibility with Objective-C. But … Read more