gpt4 book ai didi

Swift 编译器错误,表达式类型 'Error' 在没有更多上下文的情况下不明确

转载 作者:行者123 更新时间:2023-11-30 12:55:18 25 4
gpt4 key购买 nike

typealias SwiftAMapCompletion = (CLLocation?,AMapLocationReGeocode?,Error) -> Void
var locationResult : SwiftAMapCompletion?

我想给出一个 nil 作为错误,但是“Swift Compiler Error”是

Expression type 'Error' is ambiguous without more context.(SwiftAMapCompletion can't change)

locationResult!(location, reGeocode, nil as! Error)

最佳答案

你不能强制 nil 成为错误,即使你使用 as! 也不行。 .

您的选择是:

  1. 将声明更改为 Error? ,因为这意味着您可以传递 nil。
  2. 传递错误。

例如:

enum MyError: Error {
case ok
}
locationResult!(location, reGeocode, MyError.ok)

在我看来,你的SwiftAMapCompletion接口(interface)没有任何意义,因为通常这样的回调将是“这是结果,或者这是错误”,因此所有参数都应声明为可选(使用 ? )。如果可以的话我会改变这个界面。

关于Swift 编译器错误,表达式类型 'Error' 在没有更多上下文的情况下不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40479542/

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