将 cllocation 值数组存储在 userdefaults iphone sdk 中

发布于 2024-10-18 17:21:02 字数 328 浏览 6 评论 0原文

您好,我有一个 cllocation 值数组,我需要将该数组存储在 userdefaults 中...我该怎么做...?
您好,我将对象添加到数组中: [AM_locationMP.favArray addObject:[AM_locationMP.locationData objectAtIndex:AM_locationMP.indexno]];

其中位置数据是位置地标值的数组。

其中 maplocationVO 类用于地标 地图位置VO *当前地图位置;

[AM_delegate.locationData addObject: currentMapLocation];

hi i have an array of cllocation values, i need to store that array in userdefaults...how do i do it...?
hi these where i add object to my array:
[AM_locationMP.favArray addObject:[AM_locationMP.locationData objectAtIndex:AM_locationMP.indexno]];

where location data is array of location placemark values .

where maplocationVO class is for placemark
MapLocationVO *currentMapLocation;

[AM_delegate.locationData addObject: currentMapLocation];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妞丶爷亲个 2024-10-25 17:21:02

当人们显然不知道答案,或者不知道可以在 NSUserDefaults 中存储什么和不能存储什么时,为什么他们还要在这里发帖?

NSUserDefaults 类提供了访问常见类型(例如浮点数、双精度数、整数、布尔值和 URL)的便捷方法。默认对象必须是属性列表,即 NSData、NSString、NSNumber、NSDate、NSArray 或 NSDictionary 的实例(或集合的实例组合)。如果要存储任何其他类型的对象,通常应该将其存档以创建 NSData 的实例。有关更多详细信息,请参阅首选项和设置编程指南。 -- 来自文档

您需要使用 NSKeyedArchiver 将 CLLocation 数据转换为 NSData。然后使用 NSKeyedUnarchiver 将 NSData 转回 CLLocation 数据。

下一个要解决的问题是,当您归档 CLLocation 对象并将它们存储在 UserDefaults 中时,无法正确取消归档。唯一的解决方案可能是将它们保存到文件中。

Why do people post on here when they clearly don't know the answer, or understand what you can and cannot store in NSUserDefaults?

The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData. For more details, see Preferences and Settings Programming Guide. -- From the documentation

You need to use NSKeyedArchiver to turn the CLLocation data into NSData. Then use NSKeyedUnarchiver to turn the NSData back into CLLocation data.

The next problem to solve is when you archive CLLocation objects and store them in UserDefaults, the don't unarchive properly. The only solution may be to save them to file instead.

乄_柒ぐ汐 2024-10-25 17:21:02

这应该足以写它:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:yourArray forKey:@"clArray"];
[defaults synchronize];

this should be enough to write it:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:yourArray forKey:@"clArray"];
[defaults synchronize];
巡山小妖精 2024-10-25 17:21:02

使用它

来设置数组

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

  [standardUserDefaults setObject:yourArray forKey:@"array"];
  [standardUserDefaults synchronize];

和获取数组

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

  [standardUserDefaults setObject:yourArray forKey:@"array"];

NSMutableArray *array=[standardUserDefaults objectForKey:@"array"];

use this

for setting array

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

  [standardUserDefaults setObject:yourArray forKey:@"array"];
  [standardUserDefaults synchronize];

and for getting array

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

  [standardUserDefaults setObject:yourArray forKey:@"array"];

NSMutableArray *array=[standardUserDefaults objectForKey:@"array"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文