gpt4 book ai didi

ios - NSInvalidUnarchiveOperationException 仅在 iOS 扩展中引发,而不是主应用程序

转载 作者:行者123 更新时间:2023-11-28 05:29:53 25 4
gpt4 key购买 nike

我已将符合 NSCoding 的类“分配”的数组存档到共享(应用程序组)容器中的文件:

class private func updateSharedFiles() {
let path = NSFileManager().containerURLForSecurityApplicationGroupIdentifier("group.agenda-touch")!.path! + "/widgetData"
if let incompleteAssignmentsArray = self.incompleteAssignments {
NSKeyedArchiver.archiveRootObject(incompleteAssignmentsArray, toFile: path)
}
}

现在,当我的扩展程序想要从我调用 NSFileManager.fileExistsAtPath: 的文件中读取文件时,它返回 true。最后我调用 NSKeyedUnarchiver.unarchiveObjectWithFile(path) as?赋值数组并获得 NSInvalidUnarchiveOperationException。问题是,当我在我的主应用程序中取消存档文件时,我没有遇到这样的异常。需要说明的是,我已将 Assignment.swift 添加到小部件的编译源中。所以我的 TodayViewController 知道 Assignment 是什么,但由于某种原因无法对其进行解码。作为附录,这里是 Assignment 的 NSCoding 实现:

//MARK: NSCoding
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.assignmentName, forKey: "assignmentName")
aCoder.encodeObject(self.assignmentDueDate, forKey: "assignmentDueDate")
aCoder.encodeObject(self.assignmentSubject, forKey: "assignmentSubject")
aCoder.encodeBool(self.completed, forKey: "assignmentCompleted")
aCoder.encodeObject(self.creationDate, forKey: "assignmentCreationDate")
aCoder.encodeInteger(self.assignmentType.rawValue, forKey: "assignmentType")
aCoder.encodeBool(self.earmarkedForDeletion, forKey: "assignmentEarmarked")
aCoder.encodeObject(self.modificationDates, forKey: "assignmentModificationDates")
}
required convenience init(coder aDecoder: NSCoder) {
let assignmentName = aDecoder.decodeObjectForKey("assignmentName") as String
let assignmentDueDate = aDecoder.decodeObjectForKey("assignmentDueDate") as NSDate
let assignmentSubject = aDecoder.decodeObjectForKey("assignmentSubject") as String
let completed = aDecoder.decodeBoolForKey("assignmentCompleted")
self.init(name:assignmentName,dueDate:assignmentDueDate,subject:assignmentSubject,completed:completed)
self.creationDate = aDecoder.decodeObjectForKey("assignmentCreationDate") as NSDate
let assignmentTypeRaw = aDecoder.decodeIntegerForKey("assignmentType")
self.assignmentType = AssignmentType(rawValue: assignmentTypeRaw)!
self.earmarkedForDeletion = aDecoder.decodeBoolForKey("assignmentEarmarked")
self.modificationDates = aDecoder.decodeObjectForKey("assignmentModificationDates") as Dictionary<String,NSDate>
}

最佳答案

我也有同样的问题。我试图从编译源中删除和添加文件。也不工作。你找到解决办法了吗?

唯一与我的问题不同的是:我尝试取消归档对象 xxx 的 NSArray。当在主应用程序中存档和取消存档或在 WatchKit 应用程序中存档和取消存档时,它有效,但当我在主应用程序中存档它并在 WatchKit 应用程序(以及其他)中取消存档时,它不起作用。

let defaults: NSUserDefaults = NSUserDefaults(suiteName: "group.name")!

if let object: NSData = defaults.objectForKey(ArrayKey) as? NSData {
if let array: AnyObject = NSKeyedUnarchiver.unarchiveObjectWithData(object) {
return array as NSArray
}
}

return NSArray()

在我的应用程序组中通过 NSUserDefaults 交换对象是有效的。我只遇到带有对象 xxx 的 NSArray 的 NSKeyedArchiver 和 NSKeyedUnarchiver 的这个问题。

* 由于未捕获的异常“NSInvalidUnarchiveOperationException”而终止应用程序,原因:“* -[NSKeyedUnarchiver decodeObjectForKey:]: 无法解码类 xxx 的对象


编辑:我解决了使我的对象 NSSecureCoding 兼容的问题。因此我更换了

propertyName = aDecoder.decodeObjectForKey("propertyName")

propertyName = aDecoder.decodeObjectOfClass(NSString.self, forKey: "propertyName") 为 NSString

并添加了

class func supportsSecureCoding() -> Bool {
return true
}

我还在主应用程序的 applicationFinishLaunchingWithOptions 和 WatchKitApp 的第一个 WKInterfaceController 中设置了 NSKeyedUnarchiver 和 NSKeyedArchiver 的类名

    NSKeyedArchiver.setClassName("Song", forClass: Song.self)
NSKeyedUnarchiver.setClass(Song.self, forClassName: "Song")

关于ios - NSInvalidUnarchiveOperationException 仅在 iOS 扩展中引发,而不是主应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29201432/

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