dannybess
dannybess

Reputation: 599

iOS Code Works on iOS 9 but not on iOS 8?

One of my tabs (in my Tab-Based application) works on iOS 9, but does not work on iOS 8. Specifically, when trying to load data from a plist, I get the error shown below.

I have a "Planner" tab which saves entries into a plist. iOS 8 Error - reason: '*** -[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30)'

Saving Code:

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++
        }
    }

And AppDelegate preparation code:

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

}

Code in which I save items to the 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)
}

Upvotes: 20

Views: 633

Answers (1)

Ilan Kutsman
Ilan Kutsman

Reputation: 469

I'm not sure it will help but it's worth a try:

self.sections = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! Dictionary

In the "saving code" in the above mentioned line, try using NSDictionary instead of Dictionary when running on iOS8. I think I read somewhere, they only introduced Dictionary class quite recently. I'm not certain of this and I'm kind of newbie when it comes to iOS but it's worth a try...it might work. If it works, just put the Dictionary line in a if #available(ios 9.0 *) condition.

Upvotes: 1

Related Questions