Custom Table View Cell as String

Konrad Wright

I'm updating my app from Swift 2 to Swift 4 and there seems to be some lost functionality when it comes to converting a UITableViewCell to a String via:

String(MyTableViewCell)

The instance I'm attempting to use it in would be:

let cell = tableView.dequeueReusableCell(withIdentifier:String(PlayerReportCell), forIndexPath: indexPath) as! PlayerReportCell

Was this just lost functionality or is there a new way of doing this?

The error that I'm getting is..

Cannot invoke initializer for type 'String' with an argument list of type '((PlayerReportCell).Type)'
Sweeper

I don't remember being able to do it before, but in Swift 4.2, you can do:

 "\(PlayerReportCell.self)"

It's basically using string interpolation to interpolate the meta type object of the type.

Or, use String(describing:)

String(describing: PlayerReportCell.self)

Note that this relies on implementation detail and might break in a future swift version, just like your old code did. It’s better to hardcode the identifier in.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related