Tôi tạo ra một lớp chung trong nhanh chóng và tôi muốn để khởi một adapter với kiểu 'AdaptorType', nhưng tôi nhận được một lỗi biên dịchLàm thế nào để xây dựng một đặc tính theo kiểu generic trong Swift?
class Sample<ItemType, AdaptorType> {
var items = Array<ItemType>()
let adaptor = AdaptorType() //Error: 'AdaptorType' is not constructible with '()'
}
Tôi cũng đã cố gắng khởi nó trong init(), nhưng cùng vấn đề toke place
class SampleGnericClass<ItemType, AdaptorType> {
var items = Array<ItemType>()
let adaptor : AdaptorType
init() {
adaptor = AdaptorType() //It doesn't work, same error occors here
}
}
Cách thích hợp để có thuộc tính bộ điều hợp được khởi tạo với kiểu chung AdaptorType là gì? Cảm ơn bạn rất nhiều!
EDIT: mã đầy đủ về câu hỏi này
import UIKit
protocol XYManagedObject {
}
protocol XYNetworkProtocol {
typealias ManagedObjectType : XYManagedObject
func object(fromItemDictionary dictionary: NSDictionary?) -> ManagedObjectType?
func objects(fromEntryDictionary dictionary: NSDictionary?) -> Array<ManagedObjectType>?
func actionParameters(page: UInt?) -> (action: String, parameters: Dictionary<String, String>?)
func pageSize() -> UInt? // nil for unlimited
}
class ProductAdaptor : XYNetworkProtocol {
var filter = Dictionary<String, String>()
typealias ManagedObjectType = XYProductObject
func object(fromItemDictionary dictionary: NSDictionary?) -> ManagedObjectType? {
//... Will return an object here
return nil
}
func objects(fromEntryDictionary dictionary: NSDictionary?) -> Array<ManagedObjectType>? {
//... will return an array of objects here
return nil
}
func actionParameters(page: UInt?) -> (action: String, parameters: Dictionary<String, String>?) {
var parameters = filter
if page {
parameters["page"] = "\(page!)"
}
return ("product_list", parameters)
}
func pageSize() -> UInt? {
return 100
}
}
class XYDataManager<ItemType: XYManagedObject, AdaptorType: XYNetworkProtocol> {
var objects = Array<ItemType>()
let adaptor = ProductAdaptor() //work around, adaptor should be any type conform to XYNetworkProtocol
func loadPage(page: UInt?) -> (hasNextPage: Bool, items: Array<ItemType>?) {
let fetcher = XYFetchController()
let ap = adaptor.actionParameters(page)
if let fetchedResult = fetcher.JSONObjectForAPI(ap.action, parameters: ap.parameters) {
let npage = fetchedResult["npage"].integerValue
var hasNextPage = true
if !npage || !page {
hasNextPage = false
}
else if npage <= page {
hasNextPage = false
}
let objects = adaptor.objects(fromEntryDictionary: fetchedResult)
return (hasNextPage, (objects as Array<ItemType>?))
}
else {
return (false, nil)
}
}
}
bạn có thể vui lòng đăng lớp AdaptorType của mình không? – Emilie
Trừ khi AdaptorType là một chức năng/đóng cửa, nó không thể xây dựng với '()' ... – Palimondo
Để @Emilie: Các mã được đăng. Cảm ơn bạn –