MKPolyline-> NSKeyedArchiver-> NSData SIGABRT [英] MKPolyline -> NSKeyedArchiver -> NSData SIGABRT

查看:81
本文介绍了MKPolyline-> NSKeyedArchiver-> NSData SIGABRT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我试图将MKPolylines的数组存储到NSUserDefaults中.

In my app I am trying to store an array of MKPolylines into NSUserDefaults.

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:overlays];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"theKey"];

赠予:

[MKPolyline encodeWithCoder:]: unrecognized selector sent to instance 0x169c20`
Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '-
[MKPolyline encodeWithCoder:]: unrecognized 
selector sent to instance 0x1c57e0'

我取得了一些进展. MKPolylineView符合NSCoding协议,因此我已将MKPolyline s的数组转换为MKPolylineView s的数组.问题是,当我想稍后将它们添加回地图时,无法将它们转换回MKPolyline.知道怎么做吗?

I made some progress. MKPolylineView conforms to the NSCoding protocol, so I have converted my array of MKPolylines into an array of MKPolylineViews. The problem is that when I want to add them back to a map later, I can't convert them back to MKPolylines. Any idea how to do that?

在此代码处崩溃:

NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:@"theKey"];
NSArray* overlays = [NSKeyedUnarchiver unarchiveObjectWithData:data];

for(MKPolylineView* a in overlays)
    [mapView addOverlay:a.overlay];

2011-10-17 21:15:56.416 Trail Tracker[4269:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x34b2f8bf 0x36c3a1e5 0x34a8420f 0x35697595 0x6257 0x62db 0x365f77ff 0x36601d53 0x36601cc1 0x366a1339 0x366a014f 0x366fad97 0x649b 0x36671565 0x366e9ce7 0x31fcc943 0x34b03a63 0x34b036c9 0x34b0229f 0x34a854dd 0x34a853a5 0x351f9fed 0x365ec743 0x2c75 0x2c34)
terminate called throwing an exception(gdb) 

推荐答案

对此我不太确定,但是MKPolylines是由CLLocationCoordinate2D数组组成的,该数组包含lat和long的浮点值.

I am not pretty sure about this but the MKPolylines are made from the array of CLLocationCoordinate2D which contains float value for lat and long.

因此,如果您可以将此CLLocationCoordinate2D数组转换为字典数组,我认为您可以将这些行保存为用户默认设置.

So if you can convert this array of CLLocationCoordinate2D into array of dictionary I think than you can save those lines in your user defaults.

您可以做这样的事情

MKMapPoint *points = overlays.points;

NSMutableArray *temp = [NSMutableArray array];
for(int i = 0; i < points.length; i++)
{
    // Not sure for this part
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:points[i].x], @"x", [NSNumber numberWithFloat:points[i].y], @"y", nil];
    [temp addObject:dict];
}

然后,您可以使用此数组将重叠点存储在nsuserdefaults中,如下所示:

Then you can use this array to store the overlay points in nsuserdefaults like this

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:temp];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"theKey"];

这篇关于MKPolyline-&gt; NSKeyedArchiver-&gt; NSData SIGABRT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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