【问题标题】:Problem With Realm Saving Using Swift. By mistake, I said "Delete Object" directly From Realm Browser 0使用 Swift 保存领域的问题。错误地,我直接从领域浏览器 0 中说“删除对象”
【发布时间】:2020-05-15 16:23:06
【问题描述】:

为了测试如果我的领域文件中没有任何类别会发生什么,我直接删除了领域浏览器中“类别”的对象。现在,每当我从我的应用中添加新项目时,它甚至都不会在 Realm 浏览器中注册。

import UIKit
import CoreData
import RealmSwift

class CategoryViewController: UITableViewController {
    let realm = try! Realm()
    var categories: Results<Category>?

    override func viewDidLoad() {
        super.viewDidLoad()
        loadCategory()
    }

    // MARK: - Table View Datasource

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return categories?.count ?? 1
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for : indexPath)
        cell.textLabel?.text = categories?[indexPath.row].name ?? "No Categories Added Yet!"
        return cell
    }

    // MARK: - Table View Delegate

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "goToItems", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destinationVC = segue.destination as! ToDoListViewController
        if let indexPath = tableView.indexPathForSelectedRow {
            destinationVC.selectCategory = categories![indexPath.row]
        }
    }

    // MARK: - Data Manipulation Methods

    func save(category: Category) {
        do {
            try realm.write {
                realm.add(categories!)
            }
        } catch {
            print("There was an error saving context, \(error.localizedDescription)")
        }

        tableView.reloadData()
    }

    func loadCategory() {
        categories = realm.objects(Category.self)
        tableView.reloadData()
    }

    @IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
        var textField = UITextField()

        let alert = UIAlertController(title: "Add New Category", message: "", preferredStyle: .alert)

        let action = UIAlertAction(title: "Add", style: .default) { (action) in
            let newCategory = Category()
            newCategory.name = textField.text!
            self.save(category: newCategory)
        }

        alert.addAction(action)
        alert.addTextField { (field) in
            textField = field
            textField.placeholder = "Add A New Category"
        }

        present(alert, animated: true, completion: nil)
    }
}

当我在我的类别中添加称为“购物”的内容时:

点击添加按钮后:

添加“购物”后的我的 Realm 浏览器:

请记住,我之前也将对象添加到领域文件中,所以即使它没有出现在领域文件中,“还没有添加类别”甚至没有出现......那里绝对是一些问题。我没有从调试控制台收到任何错误。

【问题讨论】:

  • 大家好,如果您能帮我解决这个问题,我将不胜感激......再次非常感谢......
  • 感谢 Tyler 让这个问题更具可读性!
  • Hmmm.... 看到这个realm.add(categories!) 你保存了 empty 类变量,而它应该是这个realm.add(category)
  • 嘿,非常感谢杰伊的帮助!它有效!

标签: ios swift realm


【解决方案1】:

我认为您错误地将“类别”添加到领域中。在save()函数中,只需要将“categories”替换为“category”即可。

【讨论】:

  • 很好(见我对这个问题的评论)。一般来说,如果这是一个错字或不是真正的编码问题/问题,最好将其作为评论而不是答案发布。答案通常最适合帮助未来的读者,而寻找错字也不会有太大帮助。仍然是一个很好的收获;-)
  • 感谢@Jay 提供的宝贵信息。但我认为答案对未来的读者来说是一件好事。通常读者只注意到两件事。首先,他们注意到问题,其次他们快速向下滚动到答案列表。如果有人没有找到任何合适的答案,那么他们就会去找 cmets。
  • 酷。我投票结束了这个问题,因为它只是一个错字,可能对未来的读者没有太大帮助。
  • 没问题@Tejas
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多