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

时间:2018-05-18 09:55:12

标签: swift generics swift-protocols

继续我previous question的答案之后,我有2个协议......

protocol Filters: Encodable { 
}

protocol QueryParameters: Encodable {
    associatedtype T: Filters
    var page: Int { get }
    var filters: T { get }
}

然后对于Transaction类型,我有......

struct TransactionFilters: Filters {
    var isWithdrawal: Bool
}

struct TransactionParamters<T: Filters>: QueryParameters {
    var page: Int
    var filters: T
}

到目前为止一切都很好。

接下来我添加一个协议Filterable,我希望任何符合Filterable的类型都能够返回参数,就像这样......

protocol Filterable {
    func parameters() -> QueryParameters
}

struct Transactions: Filterable {
    func parameters() -> QueryParameters {
        let transactionFilters = TransactionFilters(isWithdrawal: true)
        return TransactionParamters(page: 1, filters: transactionFilters)
    }
}

但我最终还是......

  

错误:协议'QueryParameters'只能用作通用约束,因为它具有Self或相关类型要求

这似乎是一个非常简单的要求,但我花了两天时间尝试了我能想到的所有组合才能让它发挥作用。现在我终于承认失败了。

我需要做些什么来解决这个问题?

1 个答案:

答案 0 :(得分:4)

正如我在评论中提到的那样。您的代码中缺少的是associatedtype实际上从未成为类型。您structs之一的代码中没有任何地方您将类型分配给associatedtype。如果你想要一个通用的filterable功能,你可以沿着这些方向做点什么:

// Your generic Filters with required properties
protocol Filters: Encodable {
    var isWithdrawal: Bool { get }
    init(isWithdrawal: Bool)
}

// Your generic query parameters
protocol QueryParameters: Encodable {
    associatedtype F: Filters
    var page: Int { get }
    var filters: F { get }

    init(page: Int, filters: Filters)
}

// Filterable protocol will eventually accept any types conforming to Filters and QueryParameters
protocol Filterable {
    associatedtype F: Filters
    associatedtype P: QueryParameters

    func parameters() -> P
}

// This is your generic Transactions struct
// With this you will be able to pass other types that meet the constraints
struct Transactions<ParameterType: QueryParameters>: Filterable {
    typealias P = ParameterType
    typealias F = ParameterType.F

    func parameters() -> ParameterType {
        return P(page: 1, filters: F(isWithdrawal: true))
    }
} 

你完成了通用的东西。现在,您可以创建符合协议的不同模型对象

struct TransactionFilters: Filters {
    private(set) var isWithdrawal: Bool // Conforming to filters
}

struct TransactionParameters<FilterType: Filters>: QueryParameters {
    // Telling what type is the object that conforms to Filters
    typealias F = FilterType
    var page: Int
    var filters: FilterType

    init(page: Int, filters: Filters) {
        self.page = page
        self.filters = filters as! F
    }
}

像这样创建您的交易对象:

let transactions = Transactions<TransactionParameters<TransactionFilters>>()
print(transactions.parameters().page)
print(transactions.parameters().filters.isWithdrawal)

您可以创建更多类型的QueryParameters和过滤器

struct SomeOtherParameters<FilterType: Filters>: QueryParameters {
    // You can do custom stuff in your SomeOtherParameters struct
    // e.g. add an offset to page
    typealias F = FilterType
    var page: Int
    var filters: FilterType

    init(page: Int, filters: Filters) {
        self.page = page + 100
        self.filters = filters as! F
    }
}

// Your special filter that always returns false
struct SomeOtherFilters: Filters {
    init(isWithdrawal: Bool) {}

    var isWithdrawal: Bool {
        return false
    }
}

let transactionsWithDifferentFilters = Transactions<SomeOtherParameters<SomeOtherFilters>>()

// You can combine any types that conform to you declared protocols
let evenMoreTransactions = Transactions<SomeOtherParameters<TransactionFilters>>()
print(evenMoreTransactions.parameters().page)