danielavila
danielavila

Reputation: 144

Dynamic typealias Swift 3

I'm trying to set a typealias with a conditional like this:

typealias dataType = (x == y ? ButtonTableCell : InputTableCell)

Where both ButtonTableCell and InputTableCell are classes, so later I can use dataType like this:

let cell = tableView.dequeueReusableCell(withIdentifier: "buttonCell", for: indexPath as IndexPath) as! dataType

I don't know if this is possible in Swift

Upvotes: 4

Views: 1589

Answers (1)

Daniel T.
Daniel T.

Reputation: 33967

It's not possible. However, you can make a protocol or generics to get close to it.

protocol TableCell { 
    // put stuff here
}

extension ButtonTableCell: TableCell { }
extension InputTableCell: TableCell { }

class MyClass<X: TableCell> {
    typealias dataType = X
}

Upvotes: 4

Related Questions