将单元附件保存到NSUserDefaults [英] Saving cell accessory to NSUserDefaults

查看:44
本文介绍了将单元附件保存到NSUserDefaults的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过一个有关保存的问题,此处电池附件的状态.我现在在玩NSUserDefaults.我已经在应用程序中成功保存了许多选项,但是似乎无法将单元附件的状态保存到NSUserDefaults.

I've previously asked a question which was answered here regarding saving the state of of a cell accessory. I'm now playing with NSUserDefaults. I've successfully saved a number of options in my app but can't seem to save the state of the cell accessory to NSUserDefaults.

经过多次尝试,我得出的结论是我不知道要保存什么或在哪里保存以及不知道在何处调用保存的选项.

After a number of tries, I've come to the conclusion I don't know what or where to save as well as where to recall the saved options.

有人能指出我正确的方向吗?

Can anyoone point me in the right direction?

谢谢

虽然我知道该变量可能需要转换,但是我很难确定什么需要转换以及何时转换.

Whilst I understand the variable will likely need to be converted, I'm having a hard time what needs to be converted and when.

推荐答案

在引用代码段的问题中,请使用名为 _accessoryStyle 的实例变量.该变量的类型为 NSMutableArray .假设您仍然有此变量,只需将其内容保存为用户默认值,如下所示:

In the question that you cite the code snippets use an instance variable named _accessoryStyle. The variable has the type NSMutableArray. Assuming that you still have this variable, you can simply save its content to the user defaults like this:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:self._accessoryStyle forKey:@"aKeyName"];

如果以后要检索数据,可以这样做:

If you later want to retrieve the data, you can do it like this:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* immutableData = [userDefaults arrayForKey:@"aKeyName"]];
[self._accessoryStyle setArray:immutableData];

检索稍微复杂些,因为 arrayForKey:返回一个不变的 NSArray ,而不是 NSMutableArray .

Retrieving is slightly more complicated because arrayForKey: returns an immutable NSArray, not an NSMutableArray.

这篇关于将单元附件保存到NSUserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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