gpt4 book ai didi

ios - 使用 NSCoding 将对象解码为 Int 时抛出的错误是 : fatal error: unexpectedly found nil while unwrapping an Optional value

转载 作者:可可西里 更新时间:2023-11-01 02:00:55 27 4
gpt4 key购买 nike

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

如果应用程序在保存任何数据之前运行,它运行良好,当运行编码功能时,我的打印输出确认卷号似乎已正确保存(在我的示例中为 1 和 2)。但是,当我重新加载应用程序时,它会退出并显示错误: fatal error :在展开可选值时意外发现 nil 并突出显示:let volumeNumber = aDecoder.decodeObject(forKey: "volumeNumber") as!诠释

对我来说,应用程序似乎正在保存 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")
}
}

这是我的 View Controller ,它试图保存/加载数据: 导入 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]

}
}

最佳答案

万一其他人遇到同样的问题,问题出在我如何编码我的数据上。特别是不将我的 Int 编码为使用 encodeCInt 等,请参阅解决此问题的代码更改:

// 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")
}

关于ios - 使用 NSCoding 将对象解码为 Int 时抛出的错误是 : fatal error: unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46393956/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com