相关疑难解决方法(0)

协议只能用作通用约束,因为它具有Self或associatedType要求

我有一个协议RequestType,它有关联类型模型,如下所示.

public protocol RequestType: class {

    associatedtype Model
    var path: String { get set }

}

public extension RequestType {

    public func executeRequest(completionHandler: Result<Model, NSError> -> Void) {
        request.response(rootKeyPath: rootKeyPath) { [weak self] (response: Response<Model, NSError>) -> Void in
            completionHandler(response.result)
            guard let weakSelf = self else { return }
            if weakSelf.logging { debugPrint(response) }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试为所有失败的请求排队.

public class RequestEventuallyQueue {

    static let requestEventuallyQueue = RequestEventuallyQueue()
    let queue = [RequestType]()

}
Run Code Online (Sandbox Code Playgroud)

但我得到的错误是let queue = [RequestType](),Protocol RequestType只能用作通用约束,因为它具有Self或associatedType要求.

generics ios swift swift-protocols swift2

73
推荐指数
6
解决办法
3万
查看次数

协议扩展的可编码(或可编码)不符合它

我有2个协议,Filters并且Parameters都扩展了Encodable

protocol Filters: Encodable {
    var page: Int { get }
}

protocol Parameters: Encodable {
    var type: String { get }
    var filters: Filters { get }
}
Run Code Online (Sandbox Code Playgroud)

因此,我创建了符合这些协议的结构。

struct BankAccountFilters: Filters {
    var page: Int
    var isWithdrawal: Bool
}

struct BankAccountParamters: Parameters {
    let type: String = "Bank"
    var filters: Filters
}

let baf = BankAccountFilters(page: 1, isWithdrawal: true)
let bap = BankAccountParamters(filters: baf)
Run Code Online (Sandbox Code Playgroud)

失败是因为…

错误:类型“ BankAccountParamters”不符合协议“可编码”

注意:无法自动合成“可编码”,因为“过滤器”不符合“可编码”

Filters显然确实符合Encodable …

swift swift-protocols codable

3
推荐指数
1
解决办法
3185
查看次数

标签 统计

swift ×2

swift-protocols ×2

codable ×1

generics ×1

ios ×1

swift2 ×1