我正在构建和应用程序,用于通过解析将对象保存在本地数据存储中。然后,我运行查询以检索本地数据存储中的对象,并且运行良好。但是,我想抓取对象及其中的内容,并根据存储在解析本地数据存储对象中的项目在表视图单元格中设置一些标签。例如,我制作一个具有“objectID”,“name”,“date”,“location”等属性的对象。我想做的是在主屏幕上显示一个表格视图,该视图显示名称,日期,位置等。每个单元格标签中保存在本地数据存储区中的每个项目的数量。
我知道即时通讯正确保存:
// parse location object
let parseLighthouse = PFObject(className: "ParseLighthouse")
parseLighthouse.setObject(PFUser.currentUser()!, forKey: "User")
parseLighthouse["Name"] = self.placeTitle.text
parseLighthouse["Note"] = self.placeNote.text
parseLighthouse["Locality"] = self.placeDisplay.text!
parseLighthouse["Latt"] = self.map.region.center.latitude
parseLighthouse["Longi"] = self.map.region.center.longitude
parseLighthouse["LattDelta"] = 0.5
parseLighthouse["LongiDelta"] = 0.5
parseLighthouse["Date"] = dateInFormat
parseLighthouse.pinInBackground()
parseLighthouse.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
println("Object has been saved. ID = \(parseLighthouse.objectId)")
}
当我运行查询时,无法通过运行println(object.objectForKey(“Name”))访问属性
func performQuery() {
let query = PFQuery(className: "ParseLighthouse")
query.fromLocalDatastore()
query.whereKey("User", equalTo: PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) lighthouses.")
// Do something with the found objects
if let light = objects as? [PFObject] {
for object in light {
println(object.objectId)
println(object.objectForKey("Name"))
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
因为在运行查询时,我会按预期返回对象ID和名称。
成功检索了2座灯塔。
可选(“A3OROVAMIj”)
可选(幸福)
可选(“bbyqPZDg8W”)
可选(日期测试)
我想做的是获取解析对象本地数据存储中的名称字段,并将其作为表视图控制器中单元格上的标签名称。
我不知道如何从对象访问该信息,并正确设置标签。
有谁知道这怎么可能?
最佳答案
避免使用指针大声笑一直是个好主意...因此,为什么不将userid或username保存为特定对象。
因此更改此行:
parseLighthouse.setObject(PFUser.currentUser()!, forKey: "User")
至
parseLighthouse["username"] = PFUser.currentUser().username
回答
现在,让我们创建一个结构,该结构在Controller类之外包含objt_strong对象ID和名称。
struct Data
{
var Name:String!
var id:String!
}
然后在Controller类内部,全局声明以下代码行
var ArrayToPopulateCells = [Data]()
然后,您的查询函数将如下所示:
func performQuery() {
let query = PFQuery(className: "ParseLighthouse")
query.fromLocalDatastore()
query.whereKey("User", equalTo: PFUser.currentUser()!)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) lighthouses.")
// Do something with the found objects
if let light = objects as? [PFObject] {
for object in light {
print(object.objectId)
print(object.objectForKey("Name"))
var singleData = Data()
singleData.id = object.objectId
singleData.Name = object["Name"] as! String
self.ArrayToPopulateCells.append(singleData)
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
在表格中查看numberOfRowinSection()
return ArrayToPopulateCells.count
在cellForRowAtIndexPath()中
var data = ArrayToPopulateCells[indexPath.row]
cell.textlabel.text = data.objectID
cell.detailLabel.text = data.Name
,应该是
关于ios - Swift Parse-本地数据存储并在表 View 中显示对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32429035/