在我的應用程序中,我試圖保存在地圖上的引腳,以便在用戶在終止後打開應用程序時它們在那裏。我已經將我的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];
}
在你解碼的地方,試着把NSLog _after_解碼行。 – Anna
好吧,幫助,但重新打開應用程序時,地圖上沒有顯示引腳。 –