简体   繁体   English

如何在NSUserdefaults中保存NSMutablearray

[英]How do I save NSMutablearray in NSUserdefaults

I am new to iOS development. 我是iOS开发的新手。 I have a NSMutableArray that is populated with a dictionary. 我有一个装有字典的NSMutableArray I need to save the NSMutableArray in NSUserDefaults . 我需要将NSMutableArray保存在NSUserDefaults This is the data that I have passed to my NSMutableArray : 这是我传递给NSMutableArray

{
    "sub_slots" ={
        address = "501 Balestier Road #02-01 Wai Wing Centre";
        "avail_id" = 2230;
        "branch_id" = 600;
        "branch_name" = Balestier;
        "company_description" = "At Action X, we design, produce and deliver highly interactive sports events around the world. Through innovation, we revolutionize the sports entertainment landscape with the simple goal of creating happiness for people.";
        "company_id" = 222;
        "company_name" = "Action X";
        "d_salary_from" = 7;
        "d_salary_to" = 7;
        date = "<null>";
        description = "Interview: Face to face interview period from 4th - 8th      May (in office)
Working hours: 7am - 10am and 11am - 2pm (1 hour lunch break from 10am to     11am)
Minimum 5 working days (excluding PH)

Generating sponsorship leads in Australia.

Payment method: Cheque";
        direction = "From Toa Payoh MRT Station (NS19).
Walk 109 m to Toa Payoh Interchange.
Board Bus 139, 145 at Toa Payoh Interchange, alight at Opposite Moulmein Community Center, Balestier Road, 3 stops later.
Walk 34 m to Wai Wing Centre.";
        "end_date_time" = "";
        "expired_at" = "2015-05-08T19:21:01.000+08:00";
        followed = 0;
        grade = 2;
        "incentives_and_benefits" = "{\"food\":{\"0\":\"\"},\"commission\":{\"1\":\"$2 for first 9 appointments, $5 for all appointments if minimum 10 appointments hit.\"},\"uniform\":{\"0\":\"\"},\"transport\":{\"0\":\"\"},\"extras\":{\"0\":\"\"}}";
        "is_anytime" = 1;
        "is_permanent" = 1;
        "is_quick_job" = 0;
        "job_function_name" = Telemarketer;
        latitude = "1.326446";
        location = "1.326446,103.84705";
        longitude = "103.84705";
        "main_slot_id" = 2230;
        "mandatory_requirements" =             (
            "Good communication skills in English",
            "Well groomed, pleasant and positive work attitude",
            "Need to commit for 1 month"
        );
        "offered_salary" = 7;
        "optional_requirements" =             (
        );
        "postal_code" = 329844;
        priority = 1;
        "publish_salary" = 1;
        published = 1;
        "start_date_time" = "";
        status = MA;
        "sub_slot_id" = 2230;
        "ts_end_date_time" = 0;
        "ts_expired_at" = 1431084061;
        "ts_start_date_time" = 0;
        zone = central;
       };
    },
 }

I've tried saving it using: 我尝试使用以下方法保存它:

[[NSUserDefaults standardUserDefaults] setObject:myMutableArray forKey:@"locations"];
[[NSUserDefaults standardUserDefaults] synchronize];

When i try to run the application, it crashes in this block of code. 当我尝试运行该应用程序时,它在此代码块中崩溃。 This is the error that i am getting: 这是我得到的错误:

2015-05-08 18:19:29.559 Matchimi[477:82076] Property list invalid for format: 200 (property lists cannot contain objects of type 'CFNull')
2015-05-08 18:19:29.654 Matchimi[477:82076] Attempt to set a non-property-list object ( ) for key locations.

Any help will be appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

What's happening is that "<null>" indicates that one of the values you are trying to store is of type NSNull . 发生的事情是"<null>"表示您要存储的值之一是NSNull类型。 This type cannot be stored within NSUserDefaults , so you will need to remove any key/value pairs where the value is NSNull before storing it: 此类型不能存储在NSUserDefaults ,因此在存储值之前,您需要删除所有值为NSNull键/值对:

NSMutableArray *keysToRemove = [NSMutableArray new];
NSArray *keys = [dict allKeys];
for (NSString *key in keys) {
    id value = dict[key];
    if ([value isKindOfClass:[NSNull class]])
        [keysToRemove addObject:key];
}

for (NSString *key in keysToRemove]
    [dict removeObjectForKey:key];

(this assumes that dict is a NSMutableDictionary ). (这假定dictNSMutableDictionary )。

When you read the data back in from NSUserDefaults be prepared for missing key/value pairs and if you find any you can assume their value was null. 当您从NSUserDefaults读回数据时, NSUserDefaults为丢失的键/值对做好准备,如果发现任何键/值对,则可以假定它们的值为空。

Check your date = "<null>"; 检查您的date = "<null>"; is null and with that value you can not add it into UserDefault. 为null,并且不能使用该值将其添加到UserDefault中。

You can store dictionary,array or any custom object in NSUserDefaults using: 您可以使用以下方法将字典,数组或任何自定义对象存储在NSUserDefaults中:

[[NSUserDefaults standardUserDefaults]setObject:[NSKeyedArchiver archivedDataWithRootObject:array] forKey:@"YOUR KEY"];
[[NSUserDefaults standardUserDefaults]synchronize];

& you can retrive it using: &您可以使用以下方法检索它:

[NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults]objectForKey:@"YOUR KEY"]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM