I have a Reusable protocol that has a static function static func reuseId() -> String and a protocol extension that defines the default implementation for this function. Then I implemented an extension on UITableViewCell to comply with the Reusable protocol. Now I can use the function on my TableViewCells without any problems: SomeTableViewCell.reuseId() .
I have a problem with Generics. I have a generic class that has a generic parameter of type UITableViewCell :
internal class SomeClass<CellType: UITableViewCell>: NSObject { ... }
I want to use the function specified in Reusable in my generic class on CellType , but unfortunately this does not work properly. The compiler always generates a Type 'CellType' has no member 'reuseId' .
Does anyone know why this is happening? Is there a workaround?
I am using Xcode 7.0 with Swift 2.0.
Greetings from Germany
UPDATE: Here is an example code that better shows my problem:
import UIKit protocol Reusable { static func reuseId() -> String } extension Reusable { static func reuseId() -> String { return String(self).componentsSeparatedByString(".").last! } } extension UITableViewCell: Reusable { } class SomeGenericClass<CellType: UITableViewCell> { func someFunction() { let reuseIdentifier = CellType.reuseId() } }
This code generates the above error, but I do not quite understand why this is happening. I think the main difference from the sample code that jtbandes is hosted in is using a static function.
UPDATE: The problem is fixed in Xcode 8.3 beta 2. The sample code above now works as expected (after moving it to Swift 3, of course).
source share