将单元附件保存到NSUserDefaults

编程入门 行业动态 更新时间:2024-10-28 01:13:36
本文介绍了将单元附件保存到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

本文发布于:2023-07-16 14:40:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1122712.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元   附件   NSUserDefaults

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!