我一直在搜索顶部和底部以获取有关如何执行此操作的信息。我找到了一个很棒的教程!所以我对此还是很陌生。基本上我一直在尝试将地图视图注释存储到数组中。注释是一个单独的类,它基本上覆盖/充当MKAnnotation
引脚注释。它具有三个属性:
- 注释坐标
- 注释标题
- 注释副标题
这个数组需要存储到NSUserDefaults
. 我遇到了一个问题,这是日志:
[UIMutableIndexPath setObject:forKey:]:无法识别的选择器发送到实例 0x1187b0
存储在数组中的我的 Annotation 类对象无法保存为用户默认值。所以我不得不把这个数组变成NSData
然后保存,对吧?
我有很多代码设置,只是不工作。这是我尝试所有这些的方法:
查看控制器类.m:
- (void)syncMap { // this method is called in viewWillDissapear (for running tests) and applicationDidEnterBackground in App Delegate
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject: localOverlays]; // this is the array I was talking about
[defaults setObject:data forKey:@"overlays"];
[defaults synchronize];
}
- (void)initCircles { // called in AppDelegate UIApplicationDelegate method: applicationDidFinishLaunchingWithOptions
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey: @"overlays"];
localOverlays = [NSKeyedUnarchiver unarchiveObjectWithData: data];
if (!localOverlays) {
// Either there is a problem or it is the first time opening the app
localOverlays = [[NSMutableArray alloc] init];
}
}
注意:我正在使用数组中的两个注释进行测试 ( localOverlays
)
所以,我localOverlays
可以被编码/存档(使用NSCoder
),因为它是一个NSArray
. 但是,我不得不在我的 Annotation 类中添加一些进一步的设置。在其 .h 中,它使用 toNSCoding
和MKAnnotation
: 如下所示< NSCoding, MKAnnotation>
。对不起,如果我没有使用正确的术语。这是我的.m:
- (void)encodeWithCoder:(NSCoder *)aCoder { // should only be called when app enters background state, but since that cannot log in the console, like before I set it up so it should also be called in viewWillDissapear
NSLog(@"encodeCoder called in Annotation"); // gets called twice when the view will disappear... GOOD!
[aCoder encodeObject: title forKey: @"title"];
[aCoder encodeObject: subtitle forKey: @"subtitle"];
[aCoder encodeDouble: coordinate.latitude forKey: @"latitude"];
[aCoder encodeDouble: coordinate.longitude forKey: @"longitude"];
}
- (id)initWithCoder:(NSCoder *)aDecoder { // Should be called only at startup of app (not the first time you startup the app though... because data will be NULL)
NSLog(@"In the Annotation class, initWithCoder is called"); // Does get called at appropriate times twice... NICE!
self = [super init];
if (self) {
title = [aDecoder decodeObjectForKey: @"title"];
subtitle = [aDecoder decodeObjectForKey: @"subtitle"];
double lat = [aDecoder decodeDoubleForKey: @"latitude"];
double lon = [aDecoder decodeDoubleForKey: @"longitude"];
coordinate = CLLocationCoordinate2DMake(lat, lon);
}
return self;
}
如您所见,我已为存档设置了所有内容,对吗?好吧,似乎不是……因为现在在 ViewController 的 .m 中,我在 viewDidLoad 中也有这段代码:
for (Annotation *pin in localOverlays) {
if (pin) {
NSLog(@"valid pin: _CMD updateCircles");
[mapView addAnnotation: pin];
}
}
当我第一次打开我的应用程序并添加引脚时,这段代码运行良好。好的,现在我已经退出视图并退出应用程序,并从多任务栏中删除。当我重新打开它时,我在快速枚举行中遇到了崩溃:
-[NSURL countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xcd31140
所以,我所有的归档和编码设置都有问题。这里有什么问题......我知道这是一个冗长的问题,但我试图很好地构建它。我的代码设置是否完全不正确,我的代码中是否存在拼写错误/错误。谢谢大家!
更新:
我已经对坐标变量进行了编码,因此当我在 pin 出现在正确的坐标后启动应用程序时,但是当我尝试按下它来查看标题和副标题时,我得到了以下崩溃:
objc_msgSend
所以有些东西被释放了……只是一个猜测……糟糕的内存管理?什么会导致我的代码崩溃?
更新:
我已经更深入地研究了我的代码并更改了一些release
语句,只是改进了我的内存管理并进行了一些优化。现在我得到一个更具体的崩溃:
*** -[CFString length]: message sent to deallocated instance 0x147490
所以我的标题或副标题被解除分配......为什么?我检查了我的代码,它应该是绝对没问题的,特别是因为坐标很好......
更新:
我已经解决了这个问题!我来实现,坐标两个变量,纬度和经度是双精度数,数据类型......不是对象!因此,它们坚持并工作只是因为它们被复制......不像引用的对象。长话短说,我需要retain
。像这样:
title = [[aDecoder decodeObjectForKey: @"titler"] retain];
subtitle = [[aDecoder decodeObjectForKey: @"subtitler"] retain];