首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSInvalidArgumentException与NSCoding

NSInvalidArgumentException与NSCoding
EN

Stack Overflow用户
提问于 2018-12-05 00:13:25
回答 1查看 44关注 0票数 0

因此,我打算使用NSKeyedArchiver来存档数据,而在我的生命中,我无法理解抛出的错误。我对此有点陌生,但我以前也这样做过,而且实际上回收了相当多的代码,但是,它不会在这个代码上工作。我希望有人能帮忙。

这是视图控制器。

代码语言:javascript
复制
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]
}

这是小组班

代码语言:javascript
复制
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")

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-05 05:20:26

有三件事你需要改变

1.忘记添加NSCoding

代码语言:javascript
复制
class Group: NSObject, NSCoding{

2.直接保存UIImage是不可能的。将其转化为Data并保存。

代码语言:javascript
复制
let image = UIImage(named: "Bird.png")! 
let imageData = UIImageJPEGRepresentation(image, 1.0)

3.正如@rmaddy所言,请不要使用这些旧的目标-C存档工具。使用可快速编码及相关工具

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53623320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档