从 parse.com 检索信息时无法调用非函数类型 [PFObject] 的值
Cannot call value of non-function type [PFObject] when retriving info from parse.com
我是 Swift 的新手,我正在学习如何使用 parse.com 存储和检索数据。我正在从 PARSE 下载一个 PFObjects 数组,然后我需要将它变成一个结构,所以我创建了这个函数:
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: { [=11=].fec.compare(.fec) == .orderedAscending})
print(self.dataArrayOrdered)
}
但我不能 运行 因为在这一行中:
let returnedObjects = objects {
说的是"Cannot call value of non-function type [PFObject]"
我不确定如何避免这个问题,所以任何帮助将不胜感激
谢谢
let returnedObjects = objects { ... }
是一个语法错误,你可能想要可选绑定值
if let returnedObjects = objects { ... }
但 objects
是非可选的,可选绑定是不可能的。
只需将值赋给变量并去掉大括号
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: { [=12=].fec.compare(.fec) == .orderedAscending})
print(self.dataArrayOrdered)
} catch {
print("ERROR", error)
}
将所有 good 代码放在 do
块中并打印实际错误而不是简单的文字字符串 [=16] 非常重要=]
然而这是 Swift 并且有一个更智能和更方便的方法使用 map
函数
do {
let objects = try query.findObjects() as [PFObject]
self.dataArray = objects.map { MyData(gluc: Int([=13=]["meassure"] as! String)!, fec: [=13=]["fec"] as! Date, alimento: [=13=]["alim"] as! Int, comentarios: [=13=]["notes"] as! String) }
self.dataArrayOrdered = self.dataArray.sorted(by: { [=13=].fec.compare(.fec) == .orderedAscending})
print(self.dataArrayOrdered)
} catch {
print("ERROR", error)
}
我是 Swift 的新手,我正在学习如何使用 parse.com 存储和检索数据。我正在从 PARSE 下载一个 PFObjects 数组,然后我需要将它变成一个结构,所以我创建了这个函数:
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: { [=11=].fec.compare(.fec) == .orderedAscending})
print(self.dataArrayOrdered)
}
但我不能 运行 因为在这一行中:
let returnedObjects = objects {
说的是"Cannot call value of non-function type [PFObject]"
我不确定如何避免这个问题,所以任何帮助将不胜感激
谢谢
let returnedObjects = objects { ... }
是一个语法错误,你可能想要可选绑定值
if let returnedObjects = objects { ... }
但 objects
是非可选的,可选绑定是不可能的。
只需将值赋给变量并去掉大括号
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: { [=12=].fec.compare(.fec) == .orderedAscending})
print(self.dataArrayOrdered)
} catch {
print("ERROR", error)
}
将所有 good 代码放在 do
块中并打印实际错误而不是简单的文字字符串 [=16] 非常重要=]
然而这是 Swift 并且有一个更智能和更方便的方法使用 map
函数
do {
let objects = try query.findObjects() as [PFObject]
self.dataArray = objects.map { MyData(gluc: Int([=13=]["meassure"] as! String)!, fec: [=13=]["fec"] as! Date, alimento: [=13=]["alim"] as! Int, comentarios: [=13=]["notes"] as! String) }
self.dataArrayOrdered = self.dataArray.sorted(by: { [=13=].fec.compare(.fec) == .orderedAscending})
print(self.dataArrayOrdered)
} catch {
print("ERROR", error)
}