如何使用通用协议作为委托?
How to use a generic protocol as a delegate?
我有一个用于解析获取的数据的通用协议:
protocol DataParseDelegate: AnyObject {
associatedtype FetchResult
func didFetchData(data: FetchResult?, error: Error?)
}
extension DataParseDelegate {
func didFetchData(data: FetchResult?, error: Error?) {
// process data
}
}
在解析为特定类型的视图控制器中:
class ParentViewController<T>: UIViewController {}
class ChildViewController: ParentViewController<DataModel>, DataParseDelegate {
typealias FetchResult = DataModel
override func viewDidLoad() {
super.viewDidLoad()
FirebaseService.delegate = self
FirebaseService.shared.db.downloadData()
}
func didFetchData(data: DataModel?, error: Error?) {
// process data
}
}
我正在使用委托将获取的数据传递给视图控制器:
class FirebaseService {
static let shared = FirebaseService()
var db: Firestore! {
let settings = FirestoreSettings()
Firestore.firestore().settings = settings
let db = Firestore.firestore()
return db
}
weak var delegate: DataParseDelegate?
func downloadData() {
let first = db?.collection("myData")
.order(by: "date")
.limit(to: 8)
first?.getDocuments(completion: { [weak self] (snapshot: QuerySnapshot?, error: Error?) in
// handle errors
self?.delegate?.didFetchData(data: nil, error: error)
// parse the snapshot into DataModel
self?.delegate?.didFetchData(data: dataModel, error: nil)
}
}
}
我收到以下错误:
Protocol 'DataParseDelegate' can only be used as a generic constraint because it has Self or associate type requirements
pointing to the delegate:
weak var delegate: DataParseDelegate?
我尝试使用以下而不是 associatedtype
:
protocol DataParseDelegate: AnyObject {
func didFetchData<T>(data: [T]?, error: Error?)
}
extension DataParseDelegate {
func didFetchData<T>(data: [T]?, error: Error?) {
// process data
}
}
但是,我收到一条错误消息:
Generic parameter 'T' cannot be inferred
关于代表:
first?.getDocuments(completion: { [weak self] (snapshot: QuerySnapshot?, error: Error?) in
self?.delegate?.didFetchData(data: nil, error: error) <-----
}
对于具有关联类型的协议,编译器需要确切地知道符合对象是什么类型。
为了做到这一点,我们可以直接使用符合规范的具体类型,也可以使用协议作为通用约束。除非我们提供一些额外的上下文,否则将不知道所引用的实际类型。
所以你可以给 FirebaseService 一个具体的类型:
weak var delegate: ChildViewController?
这会起作用,但也许这不是您想要的。
请阅读 John Sundell 的这篇优秀文章:
Why can’t certain protocols, like Equatable and Hashable, be referenced directly?
我有一个用于解析获取的数据的通用协议:
protocol DataParseDelegate: AnyObject {
associatedtype FetchResult
func didFetchData(data: FetchResult?, error: Error?)
}
extension DataParseDelegate {
func didFetchData(data: FetchResult?, error: Error?) {
// process data
}
}
在解析为特定类型的视图控制器中:
class ParentViewController<T>: UIViewController {}
class ChildViewController: ParentViewController<DataModel>, DataParseDelegate {
typealias FetchResult = DataModel
override func viewDidLoad() {
super.viewDidLoad()
FirebaseService.delegate = self
FirebaseService.shared.db.downloadData()
}
func didFetchData(data: DataModel?, error: Error?) {
// process data
}
}
我正在使用委托将获取的数据传递给视图控制器:
class FirebaseService {
static let shared = FirebaseService()
var db: Firestore! {
let settings = FirestoreSettings()
Firestore.firestore().settings = settings
let db = Firestore.firestore()
return db
}
weak var delegate: DataParseDelegate?
func downloadData() {
let first = db?.collection("myData")
.order(by: "date")
.limit(to: 8)
first?.getDocuments(completion: { [weak self] (snapshot: QuerySnapshot?, error: Error?) in
// handle errors
self?.delegate?.didFetchData(data: nil, error: error)
// parse the snapshot into DataModel
self?.delegate?.didFetchData(data: dataModel, error: nil)
}
}
}
我收到以下错误:
Protocol 'DataParseDelegate' can only be used as a generic constraint because it has Self or associate type requirements pointing to the delegate:
weak var delegate: DataParseDelegate?
我尝试使用以下而不是 associatedtype
:
protocol DataParseDelegate: AnyObject {
func didFetchData<T>(data: [T]?, error: Error?)
}
extension DataParseDelegate {
func didFetchData<T>(data: [T]?, error: Error?) {
// process data
}
}
但是,我收到一条错误消息:
Generic parameter 'T' cannot be inferred
关于代表:
first?.getDocuments(completion: { [weak self] (snapshot: QuerySnapshot?, error: Error?) in
self?.delegate?.didFetchData(data: nil, error: error) <-----
}
对于具有关联类型的协议,编译器需要确切地知道符合对象是什么类型。 为了做到这一点,我们可以直接使用符合规范的具体类型,也可以使用协议作为通用约束。除非我们提供一些额外的上下文,否则将不知道所引用的实际类型。 所以你可以给 FirebaseService 一个具体的类型:
weak var delegate: ChildViewController?
这会起作用,但也许这不是您想要的。 请阅读 John Sundell 的这篇优秀文章: Why can’t certain protocols, like Equatable and Hashable, be referenced directly?