-1

As code shown below,it does not give me back my mutable array. I have 3 annotations in my mutable array, but once I close my app and open it again, it shows me 0 objects. I don't know why I am getting O objects while I am getting back my array!! any idea?

-(void)viewDidLoad
{
    NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];

    if([ud boolForKey:@"save-exist"])
    {
        NSMutableArray *udAnnotations=[[NSMutableArray alloc]initWithArray: [ud objectForKey:@"annotationsArray"]];
        NSLog(@"%d",[udAnnotations count]);
    }
    else
    {
        [self addAnno];
    }
}

-(void)addAnno
{
    [mapView addAnnotations:annotationArray];
    NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
    [ud setObject:annotationArray forKey:@"annotationsArray"];
    [ud setBool:YES forKey:@"save-exist"];
    [ud synchronize];
}
casillas
  • 16,351
  • 19
  • 115
  • 215

1 Answers1

2

You cannot save an MKAnnotation into NSUserDefaults. NSUserDefaults is a property list; only properly list objects can be stored there. You will need to archive the MKAnnotation to NSData in order to save it in NSUserDefaults. Read the docs, and also see for example Need to archive CLLocation data.

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • could you give a hint how to archive the MKAnnotation to NSData. First time I heard archiving. sorry I am very new to this environment! – casillas Oct 04 '12 at 04:08
  • 1
    and by the way, there is arrayForKey should handle the mutablearray, am I wrong? I just got that info from app documents.https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html - (NSArray *)arrayForKey:(NSString *)defaultName – casillas Oct 04 '12 at 04:11
  • 1
    It's great that you are "new to this environment". I have written an entire book on iOS programming that you might find helpful. If you learn the basics before you program, you won't ask questions that get downvoted and closed. – matt Oct 04 '12 at 15:04
  • I have already ordered your book – casillas Oct 04 '12 at 17:37