无法将数据从 NSUserDefaults 检索到今天的扩展 (Swift) [英] Cannot retrieve data from NSUserDefaults into today extension (Swift)

查看:50
本文介绍了无法将数据从 NSUserDefaults 检索到今天的扩展 (Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数组,它​​们都包含我创建的自定义 Task 对象的多个实例.我将数组保存到 NSUserDefaults 如下:

I have a group of arrays that all contain a number of instances of a custom Task object that I have created. I am saving the arrays to NSUserDefaults as follows:

自定义任务对象

class Task:NSObject, NSCoding {
    var name:String
    var notes:String
    var date:NSDate
    var dateUse:Bool
    var taskCompleted:Bool

    init(name:String, notes:String, date:NSDate, dateUse:Bool, taskCompleted:Bool){
        self.name = name
        self.notes = notes
        self.date = date
        self.dateUse = dateUse
        self.taskCompleted = taskCompleted
    }

    required init(coder decoder: NSCoder){
        self.name = decoder.decodeObjectForKey("name") as! String
        self.notes = decoder.decodeObjectForKey("notes") as! String
        self.date = decoder.decodeObjectForKey("date") as! NSDate
        self.dateUse = decoder.decodeObjectForKey("dateUse") as! Bool
        self.taskCompleted = decoder.decodeObjectForKey("taskCompleted") as! Bool
    }

    func encodeWithCoder(coder: NSCoder) {
        coder.encodeObject(self.name, forKey: "name")
        coder.encodeObject(self.notes, forKey: "notes")
        coder.encodeObject(self.date, forKey: "date")
        coder.encodeObject(self.dateUse, forKey: "dateUse")
        coder.encodeObject(self.taskCompleted, forKey: "taskCompleted")
    }
}

<小时>

保存:

let defaults = NSUserDefaults(suiteName: "group.com.myGroupName")

let nowData = NSKeyedArchiver.archivedDataWithRootObject(nowTasks)
defaults!.setObject(nowData, forKey: "nowData")

检索

let nowDataPull = defaults!.objectForKey("nowData") as? NSData
if let nowDataPull2 = nowDataPull{
   let nowTasks2 = NSKeyedUnarchiver.unarchiveObjectWithData(nowDataPull2) as? [Task]
   if let nowTasks21 = nowTasks2{
      nowTasks = nowTasks21
   }
}

上述方法适用于从 iPhone 本身设置和检索数据.但是,当尝试通过 today 扩展检索数据时,此方法不起作用.

The above method works fine for setting and retrieving data from the iPhone itself. However, this method does not work when trying to retrieve the data via the today extension.

尝试从今天扩展的 .swift 文件中检索时,我收到以下错误:

When trying to retrieve from the today extension's .swift file I get the following errors:

  • 未能从 52550 继承 CoreMedia 权限:(空)

  • Failed to inherit CoreMedia permissions from 52550: (null)

由于未捕获的异常NSInvalidUnarchiveOperationException"而终止应用程序,原因:* -[NSKeyedUnarchiver"decodeObjectForKey:]: 无法解码类的对象 (MyAppName.Task)对于关键(NS.objects);该类可以在源代码或一个未链接的库'

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '* -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (MyAppName.Task) for key (NS.objects); the class may be defined in source code or a library that is not linked'

<小时>

我知道扩展程序可以读取数据,因为当我调用时:


I know that the extension can read the data because when I call:

if (defaults?.objectForKey("nowData") != nil){
   print("there is data")
}

我得到了打印的回复..

I get the printed response..

我可以通过今天的扩展成功保存一个整数并检索,但不能objectForKey

I can successfully save an Integer and retrieve via the today extension, but not objectForKey

我尝试了各种其他保存方法,包括 .plist 文件,但似乎没有任何效果.同样的错误不断发生.任何投入将不胜感激!

I have tried various other saving methods including .plist files, but nothing seems to work. The same errors keep occurring. Any input would be greatly appreciated!

推荐答案

解决方法:

所以我为我的问题找到了一种解决方法(实际上更有效).由于对 [Task] 的解码造成了问题,我决定继续从内部没有对象的数组中检索数据,而只是 NSDate()实例.因此,我现在可以保存和访问当今扩展所需的重要数据方面.我敦促任何在自定义类解码方面遇到问题的人尽可能尝试简化您的数据,以便在您的扩展中检索和使用它.我不确定问题是什么,但从存储在 NSUserDefaults 中的数组中检索简单数据没有问题.

So I have found a workaround (which is actually more efficient) for my problem. Because the decoding of [Task] was causing problems, I decided to go ahead and retrieve the data from an array that did not have objects inside of it, but rather just NSDate() instances. Because of this I can now save and access the important aspects of data that the today extension needs. I urge anyone else who is having issues with custom class decoding to try and simplify your data if at all possible in order to retrieve and use it in your extension. I am not exactly sure what the problem was or is, but retrieving simple data from an array stored in NSUserDefaults works without problems.

希望所有这些废话可以帮助其他人!

Hope all of this nonsense can help someone else out there!

这篇关于无法将数据从 NSUserDefaults 检索到今天的扩展 (Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆