Types Conforming to Multiple Protocols in Swift

Types conforming to multiple protocols in swift

This should work:

var identityToken: NSObjectProtocol & NSCopying & NSCoding 

Note you have to use NSObjectProtocol instead of NSObject in swift.

Here are some additional examples:

Array of objects conforming to multiple protocols:

var array: [NSObjectProtocol & NSCopying & NSCoding]

Function with a parameter that conforms to multiple protocols:

func foo(param: NSObjectProtocol & NSCopying & NSCoding) {

}

For Swift version before 3.1, use:

var identityToken: (NSObjectProtocol, NSCopying, NSCoding)

Casting type conforming to multiple protocols as a single protocol

Swift cant perform a full collection type conversion (only available for some behind-the-hood automatically Objective-C-bridgeable objects, or between collections of super- and subclass elements) where the elements of the collection themselves are associated in the sense that one can be assigned to the other. You need to explicitly help out the compiler to show that element-by-element conversion is valid, e.g. using a .map operation prior to calling printNames

printNames(identifiableAndNamableItems.map{ $0 })
/* 0: Jeff
1: Fido */

Note also that you needn't go all out with multiple protocols to see this behaviour; it is likewise apparent for e.g. the following more minimal example

protocol Foo { }
struct Bar: Foo {}

let bar = Bar()
let foo: Foo = bar // ok

let barArr: [Bar] = [Bar(), Bar()]
let fooArr: [Foo] = barArr // cannot convert value of type '[Bar]' to specified type '[Foo]'
// let fooArr: [Foo] = barArr.map{ $0 } // OK

Swift: Property conforming to a specific class and in the same time to multiple protocols

You can do this with a generic class using a where clause:

A where clause enables you to require that an associated type conforms
to a certain protocol, and/or that certain type parameters and
associated types be the same.

To use it, make the class your property is defined in a generic class with a type constraint to check if the type parameter for your property matches your desired base class and protocols.

For your specific example, it could look something like this:

class MyViewController<T where T: UIView, T: Protocol1, T: Protocol2>: UIViewController {
var myView: T

// ...
}

Swift generic type conforming to two protocols

Your code:

func foo<T: UIViewController, UIPickerViewDelegate> (#viewController: T) {}

declares 2 generics parameters:

  • T which is UIViewController. And it's used as viewController parameter type.
  • UIPickerViewDelegate which is Any. And it's not used.

Instead, you should use "Where Clause", like:

func foo<T: UIViewController where T:UIPickerViewDelegate> (#viewController: T) {}

Swift class conformance of multiple protocols (XCode 7, iOS 9, Swift 2.1)

You have to implments the protocol function in the class

For example

protocol Protocol_A {
func someFunc()
}

protocol Protocol_B {
func someFuncB()
}

protocol Protocol_C {
}

protocol Protocol_D {
}


class ViewController: UIViewController, Protocol_A, Protocol_B, Protocol_C, Protocol_D {
func someFunc() {

}
func someFuncB() {

}
}

If you want function to be optional

@objc protocol Protocol_A {
optional func someFunc()
}

How can I make my CustomView conform to more than one protocol in SwiftUI?

The proper syntax for requiring a generic parameter conform to multiple protocols is to use &:

struct SequencePrinterView<T: Hashable & CustomStringConvertible>: View 

You can also use a where clause instead of putting the protocol constraints in the angle brackets:

struct SequencePrinterView<T>: View where T: Hashable, T: CustomStringConvertible

// or

struct SequencePrinterView<T>: View where T: Hashable & CustomStringConvertible


Related Topics



Leave a reply



Submit