Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Swift保存域时出现问题。错了,我说了;删除对象“;直接从领域浏览器0_Ios_Swift_Realm - Fatal编程技术网

Ios 使用Swift保存域时出现问题。错了,我说了;删除对象“;直接从领域浏览器0

Ios 使用Swift保存域时出现问题。错了,我说了;删除对象“;直接从领域浏览器0,ios,swift,realm,Ios,Swift,Realm,为了测试如果我的领域文件中没有任何类别会发生什么,我直接在领域浏览器中删除了“类别”的对象。现在,每当我从我的应用程序中添加一个新项目时,它甚至不会在领域浏览器中注册 import UIKit import CoreData import RealmSwift class CategoryViewController: UITableViewController { let realm = try! Realm() var categories: Results<Cate

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

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)
    }
}
导入UIKit
导入CoreData
导入RealmSwift
类类别ViewController:UITableViewController{
让realm=try!realm()
风险值类别:结果?
重写func viewDidLoad(){
super.viewDidLoad()
loadCategory()
}
//标记:-表视图数据源
重写func numberOfSections(在tableView:UITableView中)->Int{
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
退货类别?计数??1
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(标识符为“CategoryCell”,表示:indexath)
cell.textlab?.text=categories?[indexath.row].name???“尚未添加任何类别!”
返回单元
}
//标记:-表视图委托
重写func tableView(tableView:UITableView,didSelectRowAt indexPath:indexPath){
性能检查(带有标识符:“goToItems”,发送者:self)
}
覆盖功能准备(对于segue:UIStoryboardSegue,发送方:有吗?){
让destinationVC=segue.destination为!ToDoListViewController
如果让indexPath=tableView.indexPathForSelectedRow{
destinationVC.selectCategory=categories![indexPath.row]
}
}
//MARK:-数据操作方法
功能保存(类别:类别){
做{
试试realm.write{
realm.add(类别!)
}
}抓住{
打印(“保存上下文时出错,\(error.localizedDescription)”)
}
tableView.reloadData()
}
func loadCategory(){
categories=realm.objects(Category.self)
tableView.reloadData()
}
@iAction func addButton已按下(u发件人:UIBarButtonim){
var textField=UITextField()
let alert=UIAlertController(标题:“添加新类别”,消息:,首选样式:。警报)
让action=UIAlertAction(标题:“添加”,样式:。默认值){(操作)在
设newCategory=Category()
newCategory.name=textField.text!
self.save(类别:newCategory)
}
alert.addAction(操作)
alert.addTextField{(字段)位于
text字段=字段
textField.placeholder=“添加新类别”
}
当前(警报、动画:真、完成:无)
}
}
当我在我的分类中添加称为“购物”的内容时:

单击“添加”按钮后:

我已添加“购物”后的领域浏览器:


请记住,我以前也在领域文件中添加过对象,所以即使它没有显示在领域文件中,“尚未添加任何类别”也不会显示。。。肯定有问题。我没有从调试控制台得到任何错误。

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

嘿,伙计们,如果您能帮我解决这个问题,我将不胜感激。。。。再次非常感谢…谢谢泰勒让这个问题更具可读性!六羟甲基三聚氰胺六甲醚。。。。查看此
realm.add(categories!)
此处保存的是空类var,而它应该是此
realm.add(categority)
嘿,非常感谢您的帮助!它起作用了!!好主意(见我对问题的评论)。一般来说,如果它几乎是一个打字错误或不是真正的编码问题,那么最好将其作为评论而不是答案发布。一个答案通常最适合帮助未来的读者,而寻找一个打字错误也不会有多大帮助。仍然是一个好机会;-)感谢@Jay提供的宝贵信息。但我认为答案对未来的读者来说是件好事。通常读者只注意到两件事。首先,他们注意到问题,然后快速向下滚动到答案列表。如果有人找不到合适的答案,他们会去评论。酷。我投票决定结束这个问题,因为这只是一个打字错误,可能对未来的读者没有多大帮助