NSMutableArray to NSData Conversion using NSUserDefaults [closed]

不问归期 提交于 2019-12-13 07:59:11

问题


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];
}

回答1:


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.



来源:https://stackoverflow.com/questions/12719865/nsmutablearray-to-nsdata-conversion-using-nsuserdefaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!