shahin ali agharia
shahin ali agharia

Reputation: 1639

I can't load the data which stored in my document directory

When I am terminating the app then my data are successfully saved in document directory but when I start the app again the data is not visible.Here, its code of saving and loading of data.

func saveChecklistItems()
{
    let data = NSMutableData()

    let archiver = NSKeyedArchiver(forWritingWithMutableData: data)

    archiver.encodeObject(items, forKey: "Checklists")

    archiver.finishEncoding()

    data.writeToFile(dataFilePath(), atomically: true)
}

func loadChecklistItems()
{
    let path = dataFilePath()

    if NSFileManager.defaultManager().fileExistsAtPath(path)
    {
        if let data = NSData(contentsOfFile: path)
        {
            let unarchiver = NSKeyedUnarchiver(forReadingWithData:data)

            if let checklistItems =  unarchiver.decodeObjectForKey("ChecklistsItems") as? [ChecklistItem]
            {
                items = checklistItems
            }
            else
            {
                unarchiver.finishDecoding()
            }
        }
    }
}

Upvotes: 1

Views: 57

Answers (1)

sriram hegde
sriram hegde

Reputation: 2471

Here you have used the kay as

 archiver.encodeObject(items, forKey: "Checklists")

here you have used as ...

unarchiver.decodeObjectForKey("ChecklistsItems") as? [ChecklistItem]

change it to ....

unarchiver.decodeObjectForKey("Checklists") as? [ChecklistItem]

Upvotes: 1

Related Questions