我的一个标签(在我的基于标签的应用程序中)适用于iOS 9,但在iOS 8上不起作用。特别是,当尝试从plist加载数据时,我得到如下所示的错误。
我有一个" Planner"将条目保存到plist中的选项卡。
iOS 8错误 - reason: '*** -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30)'
保存代码:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
plistPath = appDelegate.plistPathInDocument
plistPath2 = appDelegate.plist2PathInDocument
// Extract the content of the file as NSData
let data:NSData = NSFileManager.defaultManager().contentsAtPath(plistPath)!
let data2:NSData = NSFileManager.defaultManager().contentsAtPath(plistPath2)!
do{
if(numOfViewWillAppear == 0)
{
if let x = NSKeyedUnarchiver.unarchiveObjectWithData(data2)
{
self.sortedSections = NSKeyedUnarchiver.unarchiveObjectWithData(data2) as! [String]
self.sections = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! Dictionary
}
numOfViewWillAppear++
}
}
和AppDelegate准备代码:
func preparePlist()
{
let rootPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0]
let url = NSURL(string: rootPath)
plistPathInDocument = (url?.URLByAppendingPathComponent("planner.plist").path)!
if !NSFileManager.defaultManager().fileExistsAtPath(plistPathInDocument){
let plistPathInBundle = NSBundle.mainBundle().pathForResource("planner", ofType: "plist")!
do {
try NSFileManager.defaultManager().copyItemAtPath(plistPathInBundle, toPath: plistPathInDocument)
print("plist copied")
}
catch{
print("error copying plist!")
}
}
else{
print("plst exists \(plistPathInDocument)")
}
}
我将项目保存到plist的代码:
self.sections[todoItem.dueDate] = [Assignment(name: todoItem.name, dueDate: todoItem.dueDate)]
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
plistPath = appDelegate.plistPathInDocument
do{
let sectionsData = NSKeyedArchiver.archivedDataWithRootObject(sections)
sectionsData.writeToFile(plistPath, atomically: true)
}
答案 0 :(得分:1)
我不确定它会有所帮助,但值得一试:
self.sections = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! Dictionary
在上述行中的“保存代码”中,在iOS8上运行时,请尝试使用NSDictionary
代替Dictionary
。
我想我在某个地方读过,他们最近才介绍Dictionary
课。
我不确定这一点,对于iOS来说,我有点新手,但值得一试......它可能会奏效。
如果有效,只需将Dictionary
行置于if #available(ios 9.0 *)
条件下即可。