【问题标题】:Extend super class that match Generics?扩展匹配泛型的超类?
【发布时间】:2015-11-12 04:14:03
【问题描述】:

我想在超类中使用泛型,但是当我创建子类时,我得到了错误

reference to generic type 'A' requires arguments in <...>

如何定义子类B?

import UIKit
import Foundation

var str = "Hello, playground"

protocol MyProtocol{
    func protocolfunc()
}

class A <T:MyProtocol> {

}
class B :A{
}

【问题讨论】:

    标签: ios swift generics swift2


    【解决方案1】:

    如果B 本身应该是通用的:

    class B<T:MyProtocol> : A<T> { ... }
    

    如果B 不应该是通用的,那么:

    class SomeType : MyProtocol { ... }
    class B : A<SomeType> { ... }
    

    例如:

    Welcome to Apple Swift version 2.1 (700.1.101.6 700.1.76). Type :help for assistance.
      1> class A<T> {}
      2> class B : A<Int> {}
      3>  
      4> class B<T> : A<T> {}
    

    【讨论】:

    • 工作,并全力帮助!
    • 你应该问另一个问题,因为这个问题无关。
    猜你喜欢
    • 1970-01-01
    • 2016-11-07
    • 2015-04-20
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2022-07-21
    • 1970-01-01
    相关资源
    最近更新 更多