How to convert/cast from a protocol to a class in swift?

fjlksahfob

Pretty simple, I'd have thought; I just want to check if a variable is a class and cast it to one if possible.

For example:

var cellProtocol:MyTableViewCellProtocol? = nil
cellProtocol = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier, forIndexPath: indexPath) as MyTableViewCell

how do I explicitly cast the cell to a UITableViewCell?

Inheritance as follows:

class MyTableViewCell: UITableViewCell, MyTableViewCellProtocol {
//....
}


@objc protocol MyTableViewCellProtocol: class, NSObjectProtocol {
    func configureCell()
}

That protocol definition was the result of me trying to solve this problem. My original one didn't have the @objc tag in it or the class-only identifier.

I tried a few things to make the cast happen but it has not worked:

    var cellToReturn = cellProtocol as UITableViewCell

This doesn't compile because UITableViewCell does not inherit explicitly from MyTableViewCellProtocol.

    var cellToReturn = cellProtocol as AnyObject as UITableViewCell

This fails at runtime because cellProtocol fails to cast into AnyObject.

I haven't been able to get the unsafeBitCast thing to work yet but that's another possibility I've been exploring.

Just a note, that this DOES work in Obj-C.

id<MyTableViewCellProtocol> cellProtocol = cell;
[cellProtocol configureCell];
UITableViewCell *cellCast = (UITableViewCell *)cellProtocol;

This gives me no errors and runs fine.

rintaro

With Swift 1.2 / Xcode 6.3 Beta, this compiles:

var cellToReturn = cellProtocol as! UITableViewCell

As of Swift 1.1, You have to cast it to AnyObject or Any, then UITableViewCell. I think this was a kind of bug.

var cellToReturn = cellProtocol as AnyObject as UITableViewCell

ADDED: It turns out that it's a problem of Optional

In this case, cellProtocol is MyTableViewCellProtocol?. you have to unwrap it first, then cast.

Try:

var cellToReturn = cellProtocol! as AnyObject as UITableViewCell
//                             ^

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift - How to get class that extended from protocol?

How to reference a generic class with a constraint from a Protocol with a typealias in Swift?

how do i call a protocol in one class from another in swift?

How to make a class conform to a protocol in Swift?

How to know this NSObject is class or protocol in swift?

How to implement objective C protocol in swift class?

How to conform a class to a protocol in Swift and can protocol methods be overridden?

Class to protocol conversation in swift

Swift Protocol of a particular class

Swift Protocol on a specific class?

how to return a type from a protocol in swift

Swift class doesn't conform to objective c protocol inherited from swift protocol

Swift Class Pointer as? Class Protocol?

In Swift, what does it mean for protocol to inherit from class keyword?

Swift init from unknown class which conforms to protocol

how to defined protocol function with accessing class variable in swift

Swift: How to do partial class implementation of a protocol (interface)?

How to cast controller to a specific protocol instead of a class in Swift?

In Swift how can I filter an array of objects conforming to a protocol by their class?

How to initialize a class with a property that is constrained to a generic type and a protocol in Swift

How to use a self-referencing Swift protocol as a type for a generic class

When to use `protocol` and `protocol: class` in Swift?

Accessing Swift protocol property implemented in an Obj-C class from another Swift class

Protocol inheritance from class?

ios swift class conforming protocol

Add class property to protocol in Swift

Requiring Protocol and Class in Swift Properties

Swift Property that conforms to a Protocol and Class

Swift protocol & weak references with class