我试图找出如何从字典中删除项目的方式。
我有一个从一个对象(PFO的PFObject)获取一个数组的表视图,我现在想知道如何从给定索引路径的数组中删除一个项目。
我想要删除中的项目
表视图的func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
方法。
答案 0 :(得分:1)
最终让它工作(我认为我的问题可能与我正在运行的Xcode beta的早期版本有关。
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if (editingStyle == UITableViewCellEditingStyle.Delete) {
let object: PFObject = self.array.objectAtIndex(indexPath.row) as! PFObject
object.deleteInBackground()
array?.removeObjectAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
答案 1 :(得分:0)
array.removeAtIndex(indexPath.row)
答案 2 :(得分:0)
对于parse对象中的数组属性,请使用API中提供的数组访问器来删除,例如,请参阅removeObject:forKey:
here。
请注意,remove方法接受一个对象 - 而不是索引路径 - 和一个键。它们的键是数组属性的属性名称。如果只从索引路径开始,只需使用该索引路径寻址数组属性即可获取对象。
编辑例如,假设该属性名为"arrayKey"
,您只有一个名为indexPath
的索引路径(swift不是我的语言,但是.. 。):
let array = myParseObject["arrayKey"] as [String]
let object = array[indexPath.row] as String
object.removeObject(object, forKey:"arrayKey")