本文介绍了Swift泛型和协议扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个协议可重用,它有一个静态函数 static func reuseId() - >字符串和一个定义该函数默认实现的协议扩展。然后,我在 UITableViewCell 上实现了一个扩展,以符合可重用协议。我现在可以在我的TableViewCells上使用该函数,而不会产生任何问题: SomeTableViewCell.reuseId()



与泛型有关。我有一个通用类,它具有类型为 UITableViewCell 的泛型参数:

 内部类SomeClass< CellType:UITableViewCell> ;: NSObject {
...
}

我希望能够在 CellType 的泛型类中使用 Reusable 中指定的函数,但不幸的是这并不按预期工作。编译器总是生成错误 Type'CellType'没有成员'reuseId'

有人知道为什么会发生这种情况吗?是否有解决方法?



我使用的是Xcode 7.0和Swift 2.0。



来自德国的问候



更新:以下示例代码更好地显示了我的问题:

  import UIKit 

协议可重用{
static func reuseId() - >字符串
}

扩展可重用{
static func reuseId() - > String {
return String(self).componentsSeparatedByString(。)。last!



扩展UITableViewCell:可重用{}

类SomeGenericClass< CellType:UITableViewCell> {
func someFunction(){
let reuseIdentifier = CellType.reuseId()
}
}

此代码会产生上述错误,但我不太明白为什么会发生这种情况。我认为 jtbandes 发布的示例代码的主要区别在于我使用了静态函数。




更新:此问题已在Xcode 8.3 beta 2中解决。上面的示例代码现在按预期工作当然是Swift 3)。

解决方案

这是一个有趣的问题。你的代码看起来应该可以工作。您可能需要。



这里是一个似乎正常工作的解决方法:

  class SomeGenericClass< CellType:Cell> {
func someFunction(){
let reuseIdentifier =(CellType.self as Reusable.Type).reuseId()
}
}


I have a protocol Reusablethat has a static function static func reuseId() -> String and a protocol extension that defines the default implementation for the function. Then, I implemented a extension on UITableViewCell to conform to the Reusable protocol. I can now use the function on my TableViewCells without a problem: SomeTableViewCell.reuseId().

The Problem I have is with Generics. I have a generic class that has a generic parameter of the type UITableViewCell:

internal class SomeClass<CellType: UITableViewCell>: NSObject { 
    ...
}

I want to be able to use the function specified in Reusable in my generic class on CellType but unfortunately this does not work as expected. The compiler always generates the error Type 'CellType' has no member 'reuseId'.

Does anybody 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 some sample 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 will generate the above error but I do not quite understand why this happens. I think the main difference to the sample code that jtbandes posted is that I use a static function.


UPDATE: The issue is fixed in Xcode 8.3 beta 2. The sample code above now works as expected (after migrating it to Swift 3 of course).

解决方案

That's an interesting problem. Your code seems like it should work; you might want to file an enhancement request.

Here's a workaround that seems to work correctly:

class SomeGenericClass<CellType: Cell> {
    func someFunction() {
        let reuseIdentifier = (CellType.self as Reusable.Type).reuseId()
    }
}

这篇关于Swift泛型和协议扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 03:59