2013-01-12 73 views
2

在我的應用程序中,我試圖保存在地圖上的引腳,以便在用戶在終止後打開應用程序時它們在那裏。我已經將我的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]; 
} 
+2

在你解碼的地方,試着把NSLog _after_解碼行。 – Anna

+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應該是