无法将类型“(ViewController) -> () -> ()”的值转换为预期的参数类型“() -> ()”
Cannot convert value of type '(ViewController) -> () -> ()' to expected argument type '() -> ()'
我有一个 class 有一个接受闭包的函数。
class MyFetcher {
public func fetchData(searchText: String,
onResponse: @escaping () -> (),
showResult: @escaping (String) -> ())
}
}
如下调用即可
class ViewController: UIViewController {
private func fetchData(searchText: String) {
wikipediaFetcher.fetchData(searchText: searchText,
onResponse: stopIndicationAnimation,
showResult: showResult)
}
private func stopIndicationAnimation() {
// Do something
}
private func showResult(data: String) {
// Do something
}
}
然而,当我将闭包更改为 MyFetcher
的 class 参数时,如下所示
class MyFetcher {
private let onResponse: () -> ()
private let showResult: (String) -> ()
init (onResponse: @escaping () -> (),
showResult: @escaping (String) -> ()) {
self.onResponse = onResponse
self.showResult = showResult
}
public func fetchData(searchText: String)
}
}
如下调用它会给出错误说明
Cannot convert value of type '(ViewController) -> () -> ()' to expected argument type '() -> ()'
class ViewController: UIViewController {
private let wikipediaFetcher = WikipediaFetcher(
onResponse: stopIndicationAnimation, // Error is here
showResult: showResult // Error is here
)
private func stopIndicationAnimation() {
// Do something
}
private func showResult(data: String) {
// Do something
}
我做错了什么吗?
发生此错误是因为您在 wikipediaFetcher
可用之前将 wikipediaFetcher
初始化为 ViewController
的 属性。尝试将其加载为 lazy
class ViewController: UIViewController {
private lazy var wikipediaFetcher = WikipediaFetcher(
onResponse: stopIndicationAnimation,
showResult: showResult
)
private func stopIndicationAnimation() {
// Do something
}
private func showResult(data: String) {
// Do something
}
}
我有一个 class 有一个接受闭包的函数。
class MyFetcher {
public func fetchData(searchText: String,
onResponse: @escaping () -> (),
showResult: @escaping (String) -> ())
}
}
如下调用即可
class ViewController: UIViewController {
private func fetchData(searchText: String) {
wikipediaFetcher.fetchData(searchText: searchText,
onResponse: stopIndicationAnimation,
showResult: showResult)
}
private func stopIndicationAnimation() {
// Do something
}
private func showResult(data: String) {
// Do something
}
}
然而,当我将闭包更改为 MyFetcher
的 class 参数时,如下所示
class MyFetcher {
private let onResponse: () -> ()
private let showResult: (String) -> ()
init (onResponse: @escaping () -> (),
showResult: @escaping (String) -> ()) {
self.onResponse = onResponse
self.showResult = showResult
}
public func fetchData(searchText: String)
}
}
如下调用它会给出错误说明
Cannot convert value of type '(ViewController) -> () -> ()' to expected argument type '() -> ()'
class ViewController: UIViewController {
private let wikipediaFetcher = WikipediaFetcher(
onResponse: stopIndicationAnimation, // Error is here
showResult: showResult // Error is here
)
private func stopIndicationAnimation() {
// Do something
}
private func showResult(data: String) {
// Do something
}
我做错了什么吗?
发生此错误是因为您在 wikipediaFetcher
可用之前将 wikipediaFetcher
初始化为 ViewController
的 属性。尝试将其加载为 lazy
class ViewController: UIViewController {
private lazy var wikipediaFetcher = WikipediaFetcher(
onResponse: stopIndicationAnimation,
showResult: showResult
)
private func stopIndicationAnimation() {
// Do something
}
private func showResult(data: String) {
// Do something
}
}