在我的应用程序中,我正在尝试保存地图上的图钉,以便在用户终止后打开应用程序时它们就在那里。我已经将我的mkAnnotation类符合NSCoding,并实现了两个必需的方法。注释都存储在单例类中的NSMutableArray中,所以我真的只是想在单例类中保存数组。一切都编码正常,但我不认为它们正在被解码。这是一些代码: 这是我的MKAnnotation类:
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface MapPoint : NSObject <MKAnnotation, NSCoding>
{
}
- (id)initWithAddress:(NSString*)address
coordinate:(CLLocationCoordinate2D)coordinate
title:(NSString *)t;
@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
//This is an optional property from MKAnnotataion
@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;
@property (nonatomic) BOOL animatesDrop;
@property (nonatomic) BOOL canShowCallout;
@property (copy) NSString *address;
@property (nonatomic, copy) NSString *imageKey;
@property (nonatomic, copy) UIImage *image;
@end
@implementation MapPoint
@synthesize title, subtitle, animatesDrop, canShowCallout, imageKey, image;
@synthesize address = _address, coordinate = _coordinate;
-(id)initWithAddress:(NSString *)address
coordinate:(CLLocationCoordinate2D)coordinate
title:(NSString *)t {
self = [super init];
if (self) {
_address = [address copy];
_coordinate = coordinate;
[self setTitle:t];
NSDate *theDate = [NSDate date];
subtitle = [NSDateFormatter localizedStringFromDate:theDate
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterShortStyle];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_address forKey:@"address"];
NSLog(@"ENCODING coordLatitude %f coordLongitude %f ", _coordinate.latitude, _coordinate.longitude);
[aCoder encodeDouble:_coordinate.longitude forKey:@"coordinate.longitude"];
[aCoder encodeDouble:_coordinate.latitude forKey:@"coordinate.latitude"];
[aCoder encodeObject:title forKey:@"title"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
[self setAddress:[aDecoder decodeObjectForKey:@"address"]];
NSLog(@"DECODING coordLatitude %f coordLongitude %f ", _coordinate.latitude, _coordinate.longitude);
_coordinate.longitude = [aDecoder decodeDoubleForKey:@"coordinate.longitude"];
_coordinate.latitude = [aDecoder decodeDoubleForKey:@"coordinate.latitude"];
[self setTitle:[aDecoder decodeObjectForKey:@"title"]];
}
return self;
}
@end
这是我的单身人士课程:
#import <Foundation/Foundation.h>
@class MapPoint;
@interface Data : NSObject
{
NSMutableArray *_annotations;
}
@property (retain, nonatomic) NSMutableArray *annotations;
+ (Data *)singleton;
- (NSString *)pinArchivePath;
- (BOOL)saveChanges;
@end
@implementation Data
@synthesize annotations = _annotations;
+ (Data *)singleton {
static dispatch_once_t pred;
static Data *shared = nil;
dispatch_once(&pred, ^{
shared = [[Data alloc] init];
shared.annotations = [[NSMutableArray alloc]init];
});
return shared;
}
- (id)init {
self = [super init];
if (self) {
NSString *path = [self pinArchivePath];
_annotations = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if (!_annotations) {
_annotations = [[NSMutableArray alloc]init];
}
}
return self;
}
- (NSString *)pinArchivePath {
NSArray *cachesDirectories = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [cachesDirectories objectAtIndex:0];
return [cachesDirectory stringByAppendingPathComponent:@"pins.archive"];
}
- (BOOL)saveChanges {
NSString *path = [self pinArchivePath];
return [NSKeyedArchiver archiveRootObject:[Data singleton].annotations
toFile:path];
}
@end
在地图视图控制器上的viewDidLoad方法中,我尝试将注释放在地图上的单例数组中:
for (MapPoint *mp in [Data singleton].annotations) {
[_worldView addAnnotation:mp];
}
答案 0 :(得分:1)
主要问题在于这些行中的singleton
方法:
dispatch_once(&pred, ^{
shared = [[Data alloc] init];
shared.annotations = [[NSMutableArray alloc]init]; //<-- problem line
});
shared = [[Data alloc] init];
行解码并初始化annotations
数组。
然后shared.annotations = [[NSMutableArray alloc]init];
行重新创建并重新初始化annotations
数组,从而丢弃刚刚解码的注释,以便单例始终返回一个空数组。
删除shared.annotations = [[NSMutableArray alloc]init];
行。
正如评论中已经提到的那样,导致简单混淆的另一个小问题是放置正在解码坐标的NSLog
。 解码完成后,NSLog
应为。