gpt4 book ai didi

ios - MKPolyline -> NSKeyedArchiver -> NSData SIGABRT

转载 作者:可可西里 更新时间:2023-11-01 05:47:47 24 4
gpt4 key购买 nike

在我的应用程序中,我试图将一组 MKPolylines 存储到 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 协议(protocol),因此我将我的 MKPolyline 数组转换为 MKPolylineView 数组.问题是当我想稍后将它们添加回 map 时,我无法将它们转换回 MKPolyline。知道怎么做吗?

在此代码处崩溃:

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 数组组成的,它包含纬度和经度的浮点值。

因此,如果您可以将此 CLLocationCoordinate2D 数组转换为字典数组,我认为您可以将这些行保存在用户默认值中。

你可以这样做

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中存储覆盖点

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

关于ios - MKPolyline -> NSKeyedArchiver -> NSData SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7698103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com