因此,我打算使用NSKeyedArchiver来存档数据,并且我一生无法理解所抛出的错误。我对此有些陌生,但我之前已经做过,实际上回收了很多代码,但是,在此代码上不起作用。我希望有人能提供帮助。
这是View Controller。
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return groups.count
}
func loadSampleGroup() {
let group1 = Group(name: "Spring Break", photo: #imageLiteral(resourceName: "profile"), members: ["John","Joe", "Bob"], owed: 34.56, payLog: [])
groups += [group1]
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "GroupTableViewCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? GroupTableViewCell else {
fatalError("Wrong kind of cell")
}
let group = groups[indexPath.row]
cell.groupName.text = group.name
var members = ""
for name in group.members {
members += (name+", ")
}
cell.membersLabel.text = members
cell.groupPhotoIcon.image = group.photo
return cell
}
@IBAction func unwindToGroupList(_ sender: UIStoryboardSegue) {
if let sourceViewController = sender.source as? NewGroupViewController, let group = sourceViewController.newGroup {
let newIndexPath = IndexPath(row: groups.count, section: 0)
groups.append(group)
tableView.insertRows(at: [newIndexPath], with: .automatic)
}
saveGroups()
}
func saveGroups() {
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(groups, toFile: Group.ArchiveURL.path) //***Error is Thrown here***
if isSuccessfulSave {
os_log("Groups successfully saved.", log: OSLog.default, type: .debug)
} else {
os_log("Groups failed to save...", log: OSLog.default, type: .error)
}
}
private func loadGroups() -> [Group]? {
return NSKeyedUnarchiver.unarchiveObject(withFile: Group.ArchiveURL.path) as? [Group]
}
这是小组课程
class Group: NSObject {
var name: String
var photo: UIImage
var members: [String]
var owed: Double
var payLog: [String]
init(name: String, photo: UIImage, members: [String], owed: Double, payLog: [String]) {
self.name = name
self.photo = photo
self.members = members
self.owed = owed
self.payLog = payLog
}
//Persistent save data code
struct PropertyKey {
static let name = "name"
static let photo = "photo"
static let members = "members"
static let owed = "owed"
static let payLog = "payLog"
}
//MARK: NSCoding
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: PropertyKey.name)
aCoder.encode(photo, forKey: PropertyKey.photo)
aCoder.encode(members, forKey: PropertyKey.members)
aCoder.encode(owed, forKey: PropertyKey.owed)
aCoder.encode(payLog, forKey: PropertyKey.payLog)
}
required convenience init?(coder aDecoder: NSCoder) {
let name = aDecoder.decodeObject(forKey: PropertyKey.name)
let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage
let members = aDecoder.decodeObject(forKey: PropertyKey.members)
let owed = aDecoder.decodeDouble(forKey: PropertyKey.owed)
let payLog = aDecoder.decodeObject(forKey: PropertyKey.payLog)
self.init(name: name as! String, photo: photo!, members: members as! [String], owed: owed, payLog: payLog as! [String])
}
//MARK: Archiving Paths
static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("group")
}
答案 0 :(得分:0)
您需要更改的三件事
1。您忘记添加NSCoding
class Group: NSObject, NSCoding{
2。无法直接保存UIImage
。将其隐藏到Data
中并保存。
let image = UIImage(named: "Bird.png")!
let imageData = UIImageJPEGRepresentation(image, 1.0)
3. @ rmaddy告诉我们,请不要使用这些旧的Objective-C归档工具。使用Swift Codable和相关工具