Can Objective-C code call Swift class extensions?

You can write a Swift extension and use it in Objective-C code. Tested with Xcode 6.1.1. All you need to do is: create your extension in Swift (@objc annotation needed since Swift 4.0.3) #import “ProjectTarget-Swift.h” in your Objective-C class (where “ProjectTarget” represents the XCode target the Swift extension is associated with) call the methods from … Read more

Can you override between extensions in Swift or not? (Compiler seems confused!)

It seems that overriding methods and properties in an extension works with the current Swift (Swift 1.1/Xcode 6.1) only for Objective-C compatible methods and properties. If a class is derived from NSObject then all its members are automatically available in Objective-C (if possible, see below). So with class A : NSObject { } your example … 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

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