首页 > 解决方案 > Protocol 'FlightRoutesDataProtocol' 只能用作通用约束,因为它具有 Self 或关联的类型要求

问题描述

几天前试图使用 Xcode 10 和 Swift 4.2 的应用程序,后者在 Swift 4.1 中更早,但无法解决我在使用 Swift 4.2 版本时遇到的错误。

在我的应用程序中使用此协议

protocol MyProtocol: AnyObject, RealmCollectionValue {
var name: String { get }
var count: Int { get }

static func sortKeyPath() -> String

func getTitleText() -> String
func getValueText() -> String
func getCodeText() -> String?
func getSubtitleText() -> String?
}

extension MyProtocol {

static func sortKeyPath() -> String {
    return "count"
}

func getTitleText() -> String {
    return name
}

func getValueText() -> String {
    return "\(count)"
}

func getCodeText() -> String? {
    return nil
}

func getSubtitleText() -> String? {
    return nil
}
}

它在我的应用程序的视图模型中的使用方式

private var data: [MyProtocol] = []

它给了我一个错误。

协议“MyProtocol”只能用作通用约束,因为它具有 Self 或关联的类型要求

标签: swiftgenericsprotocols

解决方案


推荐阅读