【发布时间】:2020-06-06 16:43:38
【问题描述】:
我在 swift5 中编程并想删除领域中的内容,我之前将其附加到数组中。但我不知道如何在代码中实现它。
这是用于将项目附加到列表的代码 sn-ps,但现在我需要第二个名为 deleteButtonPressed 的按钮来删除项目。
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Add new todoey item", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
//what will happen once the user clicks the Add Item button on our UIAlert
//print("Success") //print ot the debug console
//print("Add item present")
//print(textField.text)
if let currentCategory = self.selectedCategory {
do {
try self.realm.write {
let newItem = Item()
newItem.title = textField.text!
currentCategory.items.append(newItem)
}
} catch {
print("Error saving new items, \(error)")
}
}
self.tableView.reloadData()
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Create new item"
textField = alertTextField
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
【问题讨论】:
-
你试过
realm.delete()吗? -
Realm 入门指南Deleting Objects 对此进行了介绍。在发布问题之前咨询文档总是一个好主意,因为您尝试做的事情可能会被涵盖。
标签: ios swift xcode realm swift5.1