Home » Swift » Define a Swift typealias for any object that implements multiple protocols

Define a Swift typealias for any object that implements multiple protocols

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’m trying to define a typealias for a UITableViewCell’s delegate property that conforms to multiple protocols. This is what I’m trying to do and Swift complains that my syntax is wrong:

// The typealias definition
typealias CellDelegate = AnyObject<UIPickerViewDataSource, UIPickerViewDelegate>

// In my UITableViewCell subclass:
weak var delegate: CellDelegate?

“Cannot specialize the non-generic type AnyObject” is the error I’m getting. How do I do this correctly?

Answers:

The code that you posted has a different meaning from what you’d expect. You’re treating AnyObject like a generic type, with UIPickerViewDataSource and UIPickerViewDelegate as type arguments. It’s the same thing as creating a Dictionary with Int keys and String values, for example:

var someDictionary: Dictionary<Int, String>

What you’re trying to accomplish needs a different construct, called protocol composition. Swift provides it specifically to express types that conforms to multiple protocols. Its syntax is the following, you can use it anywhere you can use regular types:

FirstProtocol & SecondProtocol

Using this feature, your code would become:

// The typealias definition
typealias CellDelegate = UIPickerViewDataSource & UIPickerViewDelegate

// In my UITableViewCell subclass:
weak var delegate: CellDelegate?

Protocol composition is explained in Apple’s guide to the Swift language, here.

EDIT: Updated to Swift 3 syntax, thanks @raginmari

Questions:
Answers:

if you want to declare multiprotocol:

protocol<A, B>

Questions:
Answers:

I don’t understand why you type like thisweak var delegate: <CellDelegate>?and why you don’t typeweak var delegate: CellDelegate?

The problem is that AnyObject is generic. With the first line you try to make AnyObject non-generic,and it isn’t.

You better make a class that implement those (the datasource and the delegate).

Questions:
Answers:

With Swift 3, the syntax changed.

Until Swift 2.3:

typealias CellDelegate = protocol<UIPickerViewDataSource, UIPickerViewDelegate>

Since Swift 3:

typealias CellDelegate = UIPickerViewDataSource & UIPickerViewDelegate

Questions:
Answers:

You should pass a known object,AnyObejct is too generic,and thats why you can’t do it

Leave a Reply

Your email address will not be published. Required fields are marked *