简体   繁体   English

无法转换期望参数类型GTLServiceCompletionHandler的值

[英]Cannot convert value of expected argument type GTLServiceCompletionHandler

I wrote my app in Swift 2.3.Using Google Cloud AppEngine. 我在Swift 2.3中编写了我的应用程序。使用Google Cloud AppEngine。 Now I upgraded to xCode8.0 and need to convert my app to Swift 3.0. 现在我升级到xCode8.0并需要将我的应用程序转换为Swift 3.0。 The following code is correct in Swift 2.3. 以下代码在Swift 2.3中是正确的。 But it is wrong in Swift 3.0. 但它在Swift 3.0中是错误的。 Any help would be appreciated! 任何帮助,将不胜感激! issue: Cannot convert value of type '(GTLServiceTicket!, GTLObject!, NSError!) -> Void' to expected argument type 'GTLServiceCompletionHandler!' 问题:无法将类型'(GTLServiceTicket!,GTLObject!,NSError!) - > Void'的值转换为预期的参数类型'GTLServiceCompletionHandler!'

func sendRequest() -> GTLServiceTicket? {
    if self.query != nil && shouldSend {
        self.ticket = appService.executeQuery(query!, completionHandler: {
            (ticket: GTLServiceTicket!, object: AnyObject!, error: NSError!) -> Void in
            if error != nil {
                for handler in self.onFailure {
                    handler(ticket, error)
                }
            } else {
                for handler in self.onSuccess {
                    handler(ticket, object)
                }
            }
        })
    }
    return ticket
}

You need to do some changes to your code specially if your jumping to #Swift 3.0 #Xcode 8 如果跳转到#Swift 3.0 #Xcode 8,则需要对代码进行一些更改

things to remember :- 要记住的事情: -

  • NS is removed from most places eg NSError has become Error NS从大多数地方删除,例如NSError已成为Error

  • Also now id parameter from objective-c will have type Any in swift3. 此外,现在来自objective-c的id参数将在swift3中具有类型Any

So coming back to the question, Previously, if parameters in objective-c code did not have nullability attributes(like nonnull or nullable), Swift converts it with ! 回到这个问题,以前,如果objective-c代码中的参数没有可空属性(如非空或可空),Swift会将其转换为! making them non optional(forced unwrapping). 使它们不可选(强制展开)。 Now it convert it with ? 现在转换它? making them optional. 使它们成为可选的 Thats why you are getting an error. 这就是你收到错误的原因。 The completion handler will become : 完成处理程序将变为:

func sendRequest() -> GTLServiceTicket? { if self.query != nil && shouldSend { self.ticket = appService.executeQuery(query!, completionHandler: { (ticket: GTLServiceTicket?, object: Any?, error: Error?) -> Void in if error != nil { for handler in self.onFailure { handler(ticket, error) } } else { for handler in self.onSuccess { handler(ticket, object) } } }) } return ticket }

Thank you for your answer! 谢谢您的回答! I have tried it. 我试过了。 The following two codes are working. 以下两个代码正在运行。 All need add "as Optional 'NSError'" and "as Optional 'AnyObject'" 所有需要添加“as Optional'NSError'”和“as Optional'AnyObject'”

1. 1。

func sendRequest() -> GTLServiceTicket? {
    if self.query != nil && shouldSend {
        self.ticket = appService.executeQuery(query!, completionHandler: {
            (ticket: GTLServiceTicket?, object: Any?, error: Error?) -> Void in
            if error != nil {
                for handler in self.onFailure {
                    handler(ticket, error as Optional<NSError>)
                }
            } else {
                for handler in self.onSuccess {
                    handler(ticket, object as Optional<AnyObject>)
                }
            }
        })
    }
    return ticket
}

2. 2。

func sendRequest() -> GTLServiceTicket? {
    if self.query != nil && shouldSend {
        self.ticket = appService.executeQuery(query!) { ticket, object, error in
            if error != nil {
                for handler in self.onFailure {
                    handler(ticket, error as Optional<NSError>)
                }
            } else {
                for handler in self.onSuccess {
                    handler(ticket, object as Optional<AnyObject>)
                }
            }
        }
    }
    return ticket
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型的值转换为预期的参数Firebase - Cannot convert value of type to expected argument Firebase 无法将类型&#39;()&#39;的值转换为预期的参数&#39;()-&gt; void&#39; - cannot convert value of type '()' to expected argument '() -> void' 无法将类型&#39;String?.Type&#39;的值转换为预期的参数类型&#39;String?&#39; - Cannot convert value of type 'String?.Type' to expected argument type 'String?' 无法将“(QueryDocumentSnapshot).Type”类型的值转换为预期的参数类型“QueryDocumentSnapshot” - cannot convert value of type '(QueryDocumentSnapshot).Type' to expected argument type 'QueryDocumentSnapshot' 无法将类型“ [[ModelName]?]”的值转换为预期的参数类型“ [ModelName] .Type” - Cannot convert value of type '[[ModelName]?]' to expected argument type '[ModelName].Type' 无法将 [Type] 类型的值转换为预期的参数类型“some View” - Cannot convert value of type [Type] to expected argument type 'some View' Swift类别:“无法将类型的值转换为预期的参数类型&#39;AnyObject!&#39; - Swift category: "Cannot convert value of type to expected argument type 'AnyObject!' 无法将类型&#39;(_) - &gt; Bool&#39;的值转换为预期的参数类型&#39;NSPredicate&#39; - Cannot convert value of type '(_) -> Bool' to expected argument type 'NSPredicate' 无法将“字符串”类型的值转换为预期的参数类型“布尔” - Cannot convert value of type 'String' to expected argument type 'Bool' Swift:无法将类型&#39;() - &gt; Bool&#39;的值转换为预期的参数类型&#39;PFObject&#39; - Swift: Cannot convert value of type '() -> Bool' to expected argument type 'PFObject'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM