( )' to ' ... -> Void'-6ren"> ( )' to ' ... -> Void'-我在函数的第三行收到错误无法将“(CKAccountStatus, NSError?) -> ()”的值转换为“(CKAccountStatus, NSError?) -> Void” 。 这是代码:-6ren">
gpt4 book ai didi

Swift 3 建议更正 "cannot convert value for ' (CKAccountStatus, NSError?) -> ( )' to ' ... -> Void'

转载 作者:行者123 更新时间:2023-11-30 13:02:25 24 4
gpt4 key购买 nike

我在函数的第三行收到错误无法将“(CKAccountStatus, NSError?) -> ()”的值转换为“(CKAccountStatus, NSError?) -> Void”

这是代码:

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
container.accountStatusWithCompletionHandler{
(status: CKAccountStatus, error: NSError?) in
dispatch_async(dispatch_get_main_queue(), {
var title: String!
var message: String!
if error != nil{
title = "Error"
message = "An error occurred = \(error)"
} else {
//title = "No errors occurred getting info"
switch status{
case .Available:
message = "The user is logged in to iCloud"
title = "GOOD"
print("determined status was available")
self.shouldPullFromICloud()
//self.displayAlertWithTitle(title, message: message)
case .CouldNotDetermine:
message = "Could not determine if the user is logged" +
" into iCloud or not"
title = "BAD"
self.noUserIsSignedIn()
case .NoAccount:
message = "User is not logged into iCloud"
title = "BAD"
self.noUserIsSignedIn()
case .Restricted:
message = "Could not access user's iCloud account information"
title = "BAD"
self.noUserIsSignedIn()
}
print(title, message)
}
})
}
}

现在 Xcode 通过在倒数第二行插入“as! (CKAccountStatus, Error?) -> Void”来提供“修复它”。这没有帮助,它只是不断询问我是否要“修复它”,如果我单击它,它只会继续添加越来越多的“as!(CKAccountStatus,Error?)-> Void”。以下是单击“修复它”按钮后的样子:

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
container.accountStatusWithCompletionHandler{
(status: CKAccountStatus, error: NSError?) in
dispatch_async(dispatch_get_main_queue(), {
var title: String!
var message: String!
if error != nil{
title = "Error"
message = "An error occurred = \(error)"
} else {
//title = "No errors occurred getting info"
switch status{
case .Available:
message = "The user is logged in to iCloud"
title = "GOOD"
print("determined status was available")
self.shouldPullFromICloud()
//self.displayAlertWithTitle(title, message: message)
case .CouldNotDetermine:
message = "Could not determine if the user is logged" +
" into iCloud or not"
title = "BAD"
self.noUserIsSignedIn()
case .NoAccount:
message = "User is not logged into iCloud"
title = "BAD"
self.noUserIsSignedIn()
case .Restricted:
message = "Could not access user's iCloud account information"
title = "BAD"
self.noUserIsSignedIn()
}
print(title, message)
}
})
} as! (CKAccountStatus, Error?) -> Void
}

最佳答案

首先两件事:

accountStatusWithCompletionHandler(_:) 在 Swift 3 中已重命名为 accountStatus(completionHandler:)

func accountStatus(completionHandler: (CKAccountStatus, Error?) -> Void)

正如已经评论过的,completeHandler 的类型已经改变:

completionHandler: @escaping (CKAccountStatus, Error?) -> Void

当使用闭包表达式提供闭包参数时,您无需担心@escaping

因此,您需要替换这两行:

    container.accountStatusWithCompletionHandler{
(status: CKAccountStatus, error: NSError?) in

有了这些:

    container.accountStatus {
(status: CKAccountStatus, error: Error?) in

您可能会在 Swift 3 中发现许多其他需要修复的部分,但是修复了上述两个部分后,Xcode 会给您更好的建议。

(在测试上面的代码之前,不要忘记删除 as! (CKAccountStatus, Error?) -> Void。)

关于Swift 3 建议更正 "cannot convert value for ' (CKAccountStatus, NSError?) -> ( )' to ' ... -> Void',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39767413/

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