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

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

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

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

How to define initializers in a protocol extension?

As you can see this doesn’t work under these circumstances because when compiling, one has to make sure that all properties are initialized before using the struct/enum/class. You can make another initializer a requirement so the compiler knows that all properties are initialized: protocol Car { var wheels : Int { get set } // … 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 create swift class for category?

In Swift, you can use Extensions to add new functionality to existing classes, structs and enumeration types. They differ from Objective-C categories in a few ways, mainly: They aren’t named You don’t need to import an Extension explicitly. If you define an extension to add new functionality to an existing type, the new functionality will … Read more