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

时间:2018-10-31 22:43:16

标签: ios swift firebase firebase-realtime-database

在下面函数的第三行,出现以下错误:

  

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

我该如何解决?

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()
                }
            }
        }
    })
}

2 个答案:

答案 0 :(得分:4)

之所以不推断闭包类型,是因为未处理try语句。这意味着闭包应该会"catch"错误,但是在您的情况下,您忘记了do-try-catch规则。

因此,您可以尝试使用以下答案来捕获错误:

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
}

然后您可以断言错误(以进行测试),或者我个人会设置警报。

这样,应用程序不会崩溃,而是通知用户。我发现这非常非常有用,可以在旅途中且未插入设备的情况下-因此我可以看到错误消息,而不是无所事事的崩溃,不知道发生了什么事。

答案 1 :(得分:4)

如果在关闭主体中有不相关的编译错误,也会发生此错误。例如,您可能正在尝试比较两个或多个非布尔类型。

DECLARE @Demographic TABLE(
    [Name_ID] [int] NULL,
    [Name] [nchar](50) NULL,
    [Item_ID_1] [int] NULL,
    [Item_ID_2] [int] NULL,
    [Item_ID_3] [int] NULL,
    [Item_ID_4] [int] NULL,
    [Item_ID_5] [int] NULL
)

DECLARE @Lookup TABLE(
    [ID] [int] NULL,
    [ID_Description] [varchar](50) NULL
)

INSERT @Demographic ([Name_ID], [Name], [Item_ID_1], [Item_ID_2], [Item_ID_3], [Item_ID_4], [Item_ID_5]) VALUES (1001, N'John Dough', 701, 801, 601, NULL, NULL)
INSERT @Demographic ([Name_ID], [Name], [Item_ID_1], [Item_ID_2], [Item_ID_3], [Item_ID_4], [Item_ID_5]) VALUES (1002, N'Jane Smith', 901, 501, NULL, NULL, NULL)
INSERT @Lookup ([ID], [ID_Description]) VALUES (501, N'Horse')
INSERT @Lookup ([ID], [ID_Description]) VALUES (601, N'Mango')
INSERT @Lookup ([ID], [ID_Description]) VALUES (701, N'Trumpet')
INSERT @Lookup ([ID], [ID_Description]) VALUES (801, N'House')
INSERT @Lookup ([ID], [ID_Description]) VALUES (901, N'Cola')

;with Itemz as (select ID,ID_Description FROM @Lookup)
Select d.Name,d.Name_ID,i1.id,i1.ID_Description,i2.id,i2.ID_Description,i3.id,i3.ID_Description,i4.id,i4.ID_Description,i5.id,i5.ID_Description from @Demographic d
left Join Itemz i1 on i1.id=d.Item_ID_1
left Join Itemz i2 on i2.id=d.Item_ID_2
left Join Itemz i3 on i3.id=d.Item_ID_3
left Join Itemz i4 on i4.id=d.Item_ID_4
left Join Itemz i5 on i5.id=d.Item_ID_5

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

Unable to infer closure type in the current context