使用 NSUserDefaults 将 NSMutableArray 转换为 NSData [英] NSMutableArray to NSData Conversion using NSUserDefaults

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

问题描述

如下所示的代码,它不会返回我的可变数组.我的可变数组中有 3 个注释,但是一旦我关闭我的应用程序并再次打开它,它就会显示 0 个对象.我不知道为什么我在取回阵列时会得到 O 对象!!有什么想法吗?

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

推荐答案

您无法将 MKAnnotation 保存到 NSUserDefaults.NSUserDefaults 是一个属性列表;只有正确的列表对象才能存储在那里.您需要将 MKAnnotation 存档到 NSData 中,以便将其保存在 NSUserDefaults 中.阅读文档,并查看例如需要存档 CLLocation 数据.

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.

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

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