简体   繁体   English

无法在当前上下文中推断闭包类型

[英]Unable to infer closure type in the current context

On the 3rd line in the function below I get the following error:在下面函数的第 3 行,我收到以下错误:

Unable to infer closure type in the current context无法在当前上下文中推断闭包类型

How do I fix this?我该如何解决?

func fetchAllUsersImages() {
    print("inside func")
    self.ref.child("Posts").child(self.userID).child(self.postNum).observe(.childAdded, with: { snapshot in //error here

        var images: [URL] = []
        if let snapShotValue = snapshot.value as? [String: String] {

            for (_, value) in snapShotValue {
                if let imageURL = URL(string: value) {
                    print(imageURL, "image url here")
                    let imageAsData = try Data(contentsOf: imageURL)
                    let image = UIImage(data: imageAsData)
                    let ImageObject = Image()
                    ImageObject.image = image
                    self.arrayOfImgObj.append(ImageObject)
                    self.tableView.reloadData()
                }
            }
        }
    })
}

The reason why it is not inferring the closure type is because the try statement is not handled.之所以不推断闭包类型,是因为没有处理try语句。 This means that the closure expected to "catch" the error, but in your case, you forgot the do-try-catch rule.这意味着闭包期望"catch"错误,但在您的情况下,您忘记了do-try-catch规则。

Therefore you can try the following answer which will catch your errors:因此,您可以尝试以下答案来捕获您的错误:

do {
    let imageAsData = try Data(contentsOf: imageURL)
    let image = UIImage(data: imageAsData)
    let ImageObject = Image()
    ImageObject.image = image
    self.arrayOfImgObj.append(ImageObject)
} catch {
    print("imageURL was not able to be converted into data") // Assert or add an alert
}

You can then assert an error (for testing), or what I would personally do, is set up an alert.然后您可以断言错误(用于测试),或者我个人会做的事情是设置警报。

This way the app wouldn't crash, but instead, notify the user.这样应用程序就不会崩溃,而是通知用户。 I find this very helpful when on the go and my device isn't plugged in - so I can see the error messages instead of a blank crash with no idea what happened.我发现这在旅途中非常有用并且我的设备没有插入 - 所以我可以看到错误消息而不是空白的崩溃而不知道发生了什么。

This error can also happen if you have a non related compilation error in your closure body.如果您的闭包主体中有非相关的编译错误,也会发生此错误。 For example, you may be trying to compare two or more non-boolean types.例如,您可能正在尝试比较两个或多个非布尔类型。

extension Array where Element == Resistance {
    init(_ points: [Point]) {
        let peaks = points.beforeAndAfter { (before, current, after) -> Bool in
            before < current && current > after
        }
        self = []
    }
}

will produce Unable to infer closure type in the current context .将产生Unable to infer closure type in the current context
The correct code:正确的代码:

extension Array where Element == Resistance {
    init(_ points: [Point]) {
        let peaks = points.beforeAndAfter { (before, current, after) -> Bool in
            before.value < current.value && current.value > after.value
        }
        self = []
    }
}

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

相关问题 Alamofire无法在当前上下文中推断闭包类型 - Alamofire Unable to infer closure type in the current context 无法在SwiftUI的当前上下文中推断闭包类型 - Unable to infer closure type in the current context in SwiftUI Swift 3.0:无法推断当前上下文中的闭包类型PromiseKit - Swift 3.0: Unable to infer closure type in the current context ,PromiseKit 将文件转换为Swift 3:无法在当前上下文中推断闭包类型 - Converting a file to Swift 3: unable to infer closure type in the current context “无法在当前上下文中推断闭包参数 'b' 的类型”。 调用 function 时出现此错误 - "Unable to infer type of a closure parameter 'b' in the current context". Getting this error while calling the function Swift 无法推断复杂的闭包返回类型 - Swift unable to infer complex closure return type 无法推断出复杂的闭包返回类型; 添加显式类型以消除歧义 - Unable to infer complex closure return type; add explicit type to disambiguate 无法推断复杂的闭包返回类型 SwiftUI ForEach - Unable to infer complex closure return type SwiftUI ForEach Swift Combine:无法推断复杂的闭包返回类型错误 - Swift Combine: Unable to infer complex closure return type error SwiftUI:ForEach 无法推断复杂的闭包返回类型 - SwiftUI: ForEach Unable to infer complex closure return type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM