如何在NSUserdefaults中保存NSMutablearray

时间:2015-05-08 11:39:50

标签: ios objective-c nsuserdefaults

我是iOS开发的新手。我有一个填充了字典的NSMutableArray。我需要在NSMutableArray中保存NSUserDefaults。 这是我传递给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;
       };
    },
 }

我尝试使用以下方式保存它:

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

当我尝试运行该应用程序时,它会在此代码块中崩溃。这是我得到的错误:

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.

任何帮助将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:1)

正在发生的事情是"<null>"表示您尝试存储的值之一是NSNull类型。此类型无法存储在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];

(这假定dictNSMutableDictionary)。

当您从NSUserDefaults读回数据时,请为缺少的键/值对做好准备,如果找到任何数据,则可以假设它们的值为空。

答案 1 :(得分:0)

检查您的date = "<null>";是否为null,并且使用该值无法将其添加到UserDefault。

答案 2 :(得分:0)

您可以使用以下方法在NSUserDefaults中存储字典,数组或任何自定义对象:

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

&安培;您可以使用以下方式对其进行检索:

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