gpt4 book ai didi

ios - 在 Swift4 中扩展泛型类

转载 作者:IT王子 更新时间:2023-10-29 05:33:53 27 4
gpt4 key购买 nike

假设我们有一个简单的通用类:

class Foo<T> {

}

接下来给这个类添加一个extension实现UITableViewDatasoure :

extension Foo: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Code here
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Code here
}
}

此构造导致编译器错误并显示消息:

@objc is not supported within extensions of generic classes or classes that inherit from generic classes Non-'@objc' method

'tableView(_:numberOfRowsInSection:)' does not satisfy requirement of '@objc' protocol 'UITableViewDataSource'

谁能告诉我为什么?以及如何解决这个问题?

最佳答案

错误信息看起来很清楚。这不受支持。您不能将 @objc 方法附加到扩展中的泛型类。您需要在类定义中定义这些方法,而不是扩展。 “为什么”是“编译器今天不支持它”。 (由于特化,可能很难支持,但真正的答案是“编译器做不到。”)

关于ios - 在 Swift4 中扩展泛型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48386613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com