以NSMutableArray的NSData的转换NSUserDefaults的使用 [英] NSMutableArray to NSData Conversion using NSUserDefaults

查看:644
本文介绍了以NSMutableArray的NSData的转换NSUserDefaults的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下图所示code,它并没有给我回我的可变数组。我在我的可变数组3注解,但一旦我关闭我的应用程序,然后再次打开它,它让我0对象。我不知道为什么我收到O对象,而我取回我的阵!任何想法?

   - (无效)viewDidLoad中
{
    * NSUserDefaults的UD = [NSUserDefaults的standardUserDefaults]    如果([UD boolForKey:@拯救并存])
    {
        NSMutableArray里* udAnnotations = [[NSMutableArray里的alloc] initWithArray:[UD objectForKey:@annotationsArray]];
        的NSLog(@%D,[udAnnotations计数]);
    }
    其他
    {
        [个体经营addAnno里]
    }
} - (无效)addAnno里
{
    [图形页面addAnnotations:annotationArray];
    * NSUserDefaults的UD = [NSUserDefaults的standardUserDefaults]
    [UD的setObject:annotationArray forKey:@annotationsArray];
    [UD setBool:YES forKey:@拯救并存];
    [UD同步]
}


解决方案

您不能保存MKAnnotation NSUserDefaults的进入。是NSUserDefaults的一个财产清单;只有正确列表对象可以存储在那里。您将需要存档MKAnnotation到NSData的,以它保存在NSUserDefaults的。阅读文档,也见例如需要归档CLLocation数据

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

解决方案

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.

这篇关于以NSMutableArray的NSData的转换NSUserDefaults的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆