Ios 使用NSCoding将对象解码为Int时抛出的错误是:致命错误:在展开可选值时意外发现nil

Ios 使用NSCoding将对象解码为Int时抛出的错误是:致命错误:在展开可选值时意外发现nil,ios,swift,optional,nscoding,Ios,Swift,Optional,Nscoding,我在Swift中有一组对象,我正在使用NSCoding将它们保存到磁盘,以便下次运行应用程序时可以将它们读入内存。正在保存的项是一个卷对象数组。卷的每个实例都有一个已设置的volumeNumber Int。Volume和QA类都继承自NSObject和NSCoding 如果在保存任何数据之前运行应用程序,它会正常运行,当运行编码功能时,我的打印输出会确认卷号已正确保存(在我的示例中为1和2)。但是,当我重新加载应用程序时,它会退出,错误为:致命错误:在展开可选值时意外发现nil,并将:let v

我在Swift中有一组对象,我正在使用NSCoding将它们保存到磁盘,以便下次运行应用程序时可以将它们读入内存。正在保存的项是一个卷对象数组。卷的每个实例都有一个已设置的volumeNumber Int。Volume和QA类都继承自NSObject和NSCoding

如果在保存任何数据之前运行应用程序,它会正常运行,当运行编码功能时,我的打印输出会确认卷号已正确保存(在我的示例中为1和2)。但是,当我重新加载应用程序时,它会退出,错误为:致命错误:在展开可选值时意外发现nil,并将:let volumeNumber=aDecoder.decodeObject(forKey:“volumeNumber”)高亮显示为!Int

在我看来,应用程序似乎正在保存int,但由于某些原因,当应用程序尝试从内存中读取时,无法找到它们。有人能看出我可能做错了什么吗

这是我的卷课:

import Foundation
class Volume: NSObject, NSCoding {
    let volumeNumber: Int
    var completed: Bool
    var questionsData: [QA]

    init (volumeNumber: Int, completed: Bool, questionsData: [QA]) {
        self.volumeNumber = volumeNumber
        self.completed = completed
        self.questionsData = questionsData
    }

    func percent() -> Int {
        var correctAnswersTotal = 0
        var percentage = 0
        for question in questionsData {
            if question.correctAnswer == question.selectedAnswer {
                correctAnswersTotal += 1
            }
        }
        percentage = ((correctAnswersTotal / questionsData.count) * 100) as Int
        print("percentage is: \(percentage)")
        return percentage
    }

    // MARK: NSCoding
    public convenience required init?(coder aDecoder: NSCoder) {
        print("Trying to read volumeNumber in decoder now")
        let volumeNumber = aDecoder.decodeObject(forKey: "volumeNumber") as! Int
        let completed = aDecoder.decodeObject(forKey: "completed") as! Bool
        let questionsData = aDecoder.decodeObject(forKey: "questionsData") as! [QA]

        self.init(volumeNumber: volumeNumber, completed: completed, questionsData: questionsData)
    }

    func encode(with aCoder: NSCoder) {
        print("about to encode \(volumeNumber) in func encode")
        aCoder.encode(volumeNumber, forKey: "volumeNumber")
        aCoder.encode(completed, forKey: "completed")
        aCoder.encode(questionsData, forKey: "questionsData")
    }
}
这是我的视图控制器,它尝试保存/加载数据: 导入UIKit

class VolumeTableViewController: UITableViewController {

    static var volumesArray: [Volume] = [Volume(volumeNumber: 1, completed: false, questionsData: [QA(questionsText: "Question 1", answerText: ["Answer A", "Answer B", "Answer C", "Answer D"], correctAnswer: [true, false, false, false], selectedAnswer: [false, false, false, false])])]

    override func viewDidLoad() {
        print("view did load")
        super.viewDidLoad()
        if let loadedVolumes = VolumeTableViewController.read() {
            print("Using loaded volumes...")
            VolumeTableViewController.volumesArray = loadedVolumes
        } else {
            print("Setting up fresh set of volumes...")
            VolumeTableViewController.volumesArray = [Volume(volumeNumber: 1, completed: false, questionsData: [QA(questionsText: "Vol1 Question 1", answerText: ["Answer A", "Answer B", "Answer C", "Answer D"], correctAnswer: [true, false, false, false], selectedAnswer: [false, false, false, false])]),
            Volume(volumeNumber: 2, completed: false, questionsData: [QA(questionsText: "Vol 2 Question 1", answerText: ["Answer A", "Answer B", "Answer C", "Answer D"], correctAnswer: [true, false, false, false], selectedAnswer: [false, false, false, false])])]
        }
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return VolumeTableViewController.volumesArray.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "VolumeCell", for: indexPath)
        // Configure the cell...
        if VolumeTableViewController.volumesArray[indexPath.row].completed {
            let percent = VolumeTableViewController.volumesArray[indexPath.row].percent()
            cell.textLabel?.text = "Volume \(indexPath.row + 1) \(percent) %"
        }
                cell.textLabel?.text = "Volume \(indexPath.row + 1)"
        return cell
    }


    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }

    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToQuestionsTableView" {
            if let indexPath = self.tableView.indexPathForSelectedRow {
                print("index selected for path for volume is: \(indexPath.row)")
                let controller = segue.destination as! QuestionsTableViewController
                controller.volume = indexPath.row
            }
        }
    }
    // Mark NSCODING
    static func save() {
        print("About to save...")
        let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
        let ArchiveURL = DocumentsDirectory.appendingPathComponent("volumesData")
        NSKeyedArchiver.archiveRootObject(VolumeTableViewController.volumesArray, toFile: ArchiveURL.path)
    }

    static  func read() -> [Volume]? {
        print("About to read()")
        let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
        let ArchiveURL = DocumentsDirectory.appendingPathComponent("volumesData")
        return NSKeyedUnarchiver.unarchiveObject(withFile: ArchiveURL.path) as? [Volume]

    }
}

如果其他人遇到同样的问题,问题在于我如何对数据进行编码。特别是不使用encodeCInt等编码my Int请参见解决此问题的代码更改:

// MARK: NSCoding
public convenience required init?(coder aDecoder: NSCoder) {
    print("Trying to read volumeNumber in decoder now")
    let volumeNumber = aDecoder.decodeInteger(forKey: "volumeNumber")
    let completed = aDecoder.decodeBool(forKey: "completed")
  let questionsData = aDecoder.decodeObject(forKey: "questionsData") as! [QA]

    self.init(volumeNumber: volumeNumber, completed: completed, questionsData: questionsData)
}

func encode(with aCoder: NSCoder) {
    print("about to encode \(volumeNumber) in func encode")
    aCoder.encodeCInt(Int32(volumeNumber), forKey: "volumeNumber")
    aCoder.encode(completed, forKey: "completed")
    aCoder.encode(questionsData, forKey: "questionsData")
}

您需要使用
decodeInteger(forKey:)
,如果密钥不存在,它将返回0而不是nil。如果您仍然遇到任何问题,您可能需要将QA对象保存为数据。在这种情况下,这个答案将帮助你修复它,谢谢你的建议,但是这似乎不是问题——通过改变应用程序在下一行失败,它试图解码的原因相同。保存的整数“不应该”为零,所以我不知道它为什么会这样。只要使用
decodeBool(forKey:)
,因为
volumeNumber
是一个常规的
Int
,不需要将
Int
转换为
Int32
,只需编写
aCoder.encode(volumeNumber,forKey…