Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Typealias for Class and Protocol

Tags:

types

swift

I have a variable that must be a UIView that also implements the protocol MTMathKeyboard.

I've tried,

var keyboard: (UIView & MTMathKeyboard)
var keyboard: UIView<MTMathKeyboard>

What's the syntax for a non-generic class instance that implements a protocol?

like image 980
Jacob Parker Avatar asked Mar 15 '17 11:03

Jacob Parker


2 Answers

In Swift 4 you can use:

let keyboard: UIView & MTMathKeyboard
like image 67
Jeroen Bakker Avatar answered Oct 17 '22 05:10

Jeroen Bakker


I think you should go this way:

protocol MTMathKeyboard: class {

}

class YourClass<MTMathKeyboard where T:UIView> {
    var keyboard: T!
}
like image 26
Grzegorz Krukowski Avatar answered Oct 17 '22 05:10

Grzegorz Krukowski