gpt4 book ai didi

swift - 从 parse.com 检索信息时无法调用非函数类型 [PFObject] 的值

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

我是 Swift 新手,正在学习如何使用 parse.com 来存储和检索数据。我正在从 PARSE 下载 PFObject 数组,然后需要将其转换为结构,因此我创建了这个函数:

func queryDownload (user : PFUser) {

let objects: [PFObject]
let query = PFQuery(className: "Gluc")
query.whereKey("user", equalTo: user)
do {
objects = try query.findObjects() as [PFObject]

} catch {
print("ERROR")

}
let returnedObjects = objects {
let elements = self.returnedObjects.count
for i in 0...elements-1 {
self.dataArray.append(MyData(gluc: Int(self.returnedObjects[i]["meassure"] as! String)!, fec: self.returnedObjects[i]["fec"] as! Date, alimento: self.returnedObjects[i]["alim"] as! Int, comentarios: self.returnedObjects[i]["notes"] as! String))

}
}

self.dataArrayOrdered = self.dataArray.sorted(by: { $0.fec.compare($1.fec) == .orderedAscending})

print(self.dataArrayOrdered)
}

但我无法运行它,因为在这一行中:

let returnedObjects = objects {

它说“无法调用非函数类型[PFObject]的值”

我不知道如何避免这个问题,所以任何帮助将不胜感激

谢谢

最佳答案

let returnedObjects = objects { ... }

是一个语法错误,您可能想要可选绑定(bind)该值

if let returnedObjects = objects { ... }

但是objects是非可选的,并且可选绑定(bind)是不可能的。

<小时/>

只需将值赋给变量并删除大括号

do {
let objects = try query.findObjects() as [PFObject]
let returnedObjects = objects
let elements = self.returnedObjects.count
for i in 0...elements-1 {
self.dataArray.append(MyData(gluc: Int(self.returnedObjects[i]["meassure"] as! String)!, fec: self.returnedObjects[i]["fec"] as! Date, alimento: self.returnedObjects[i]["alim"] as! Int, comentarios: self.returnedObjects[i]["notes"] as! String))
}
self.dataArrayOrdered = self.dataArray.sorted(by: { $0.fec.compare($1.fec) == .orderedAscending})
print(self.dataArrayOrdered)
} catch {
print("ERROR", error)
}

将所有好的代码放入do block 中并打印实际错误而不是简单的文字字符串“ERROR”<非常重要

但是这是 Swift,有一种更智能、更方便的方法使用 map 函数

do {
let objects = try query.findObjects() as [PFObject]
self.dataArray = objects.map { MyData(gluc: Int($0["meassure"] as! String)!, fec: $0["fec"] as! Date, alimento: $0["alim"] as! Int, comentarios: $0["notes"] as! String) }
self.dataArrayOrdered = self.dataArray.sorted(by: { $0.fec.compare($1.fec) == .orderedAscending})
print(self.dataArrayOrdered)
} catch {
print("ERROR", error)
}

关于swift - 从 parse.com 检索信息时无法调用非函数类型 [PFObject] 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45355561/

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