Swift Extension: same extension function in two Modules

Details Swift 3, Xcode 8.1 Swift 4, Xcode 9.1 Swift 5.1, Xcode 11.2.1 Problem frameworks SwiftFoundation and SwiftKit has the same names of the properties and functions decision Way1 Use different names of the properties and functions // SwiftFoundation public extension UIView { public class func swiftFoundationSomeClassMethod() { print(“someClassMethod from Swift Foundation”) } public var … 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

Why can’t extensions with protocol conformances have a specific access level?

It’s because it’s impossible to conform to a protocol at any access level other than the access level of the protocol itself. In other words, if you have a public protocol, you cannot have private conformance to it. This is partially because protocol conformance is something that can be queried for at runtime (and therefore … 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

How to make extension for multiple classes Swift

You could make a protocol and extend it. Something like: protocol Animations { func animateHidden(flag: Bool) } extension Animations { func animateHidden(flag: Bool) { // some code } } extension UILabel: Animations {} extension UIImageView: Animations {} Your method will be available for the extended classes: let l = UILabel() l.animateHidden(false) let i = UIImageView() … Read more

what is ‘where self’ in protocol extension

That syntax is: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID521 Consider: protocol Meh { func doSomething() } // Extend protocol Meh, where `Self` is of type `UIViewController` // func blah() will only exist for classes that inherit `UIViewController`. // In fact, this entire extension only exists for `UIViewController` subclasses. extension Meh where Self: UIViewController { func blah() { print(“Blah”) } func … 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

Set (Collection) – Insert multiple elements

It was pointed out in the comments under the question, but I’d like to clearly state that there is a method for that very same purpose: mutating func formUnion<S>(_ other: S) where Element == S.Element, S : Sequence Usage: var attendees: Set = [“Alicia”, “Bethany”, “Diana”] let visitors = [“Diana”, “Marcia”, “Nathaniel”] attendees.formUnion(visitors) print(attendees) // … Read more

Swift 2 Error using mutating function in Protocol extension “Cannot use mutating member on immutable value: ‘self’ is immutable

The problem is that, in the protocol you mark the function as mutating, which you need to do if you want to use the protocol on a struct. However, the self that is passed to testFunc is immutable (it’s a reference to a instance of the class) and that is tripping up the compiler. This … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)