ios - 如何在 NSUserdefaults 中保存 NSMutablearray

标签 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.

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

最佳答案

发生的事情是"<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];

(假设 dict 是一个 NSMutableDictionary )。

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

关于ios - 如何在 NSUserdefaults 中保存 NSMutablearray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30122933/

相关文章:

ios - 如何创建自定义 SKActions?

iphone - iOS CoreLocation 检查 CLLocation 时间戳以使用它

ios - 为什么我的 UICollectionViewController 不能与两个 CollectionView 一起运行?

objective-c - 保护 NSUserDefaults 免受用户或第三方入侵

swift - UserDefaults() 和 UserDefaults.standard 之间的区别

swift - 如何在 Swift 中以字符串格式 "English"(不是 "en")显示当前语言?

ios - 当推送到另一个 ViewController 时如何隐藏 UINavigationBar?

iphone - 为什么device.multitaskingSupported在某些设备上不起作用?

objective-c - ( Objective-C )我可以在两个不同的函数中使用相同的变量名,但在同一个文件中吗?

ios - 当我有多个 iOS 部分时从 UITableView 中删除行(值在 Dynamic NSMutableDictionary 中)