• NSUserDefaults存储自定义对象


    场景是这样的:

    现在有一个数组如下,数组中存放着自定义的对象GoodscCategory

    <__NSArrayM 0x7ffb9c2032b0>(
    <GoodscCategory: 0x7ffb9c2079f0>,
    <GoodscCategory: 0x7ffb9c2229e0>,
    <GoodscCategory: 0x7ffb9c2217a0>,
    <GoodscCategory: 0x7ffb9c222c30>,
    <GoodscCategory: 0x7ffb9c21d710>,
    <GoodscCategory: 0x7ffb9c21afe0>,
    <GoodscCategory: 0x7ffb9c223ff0>,
    <GoodscCategory: 0x7ffb9c221f80>,
    <GoodscCategory: 0x7ffb9c21fcf0>,
    <GoodscCategory: 0x7ffb9c224bf0>,
    <GoodscCategory: 0x7ffb9c224c10>,
    <GoodscCategory: 0x7ffb9c21a0e0>,
    <GoodscCategory: 0x7ffb9c0a0550>
    )

    在尝试将该数组存储在NSUserDefaults时,发生了如下错误:

    Attempt to set a non-property-list object (
        "<GoodscCategory: 0x7ffb9c2079f0>",
        "<GoodscCategory: 0x7ffb9c2229e0>",
        "<GoodscCategory: 0x7ffb9c2217a0>",
        "<GoodscCategory: 0x7ffb9c222c30>",
        "<GoodscCategory: 0x7ffb9c21d710>",
        "<GoodscCategory: 0x7ffb9c21afe0>",
        "<GoodscCategory: 0x7ffb9c223ff0>",
        "<GoodscCategory: 0x7ffb9c221f80>",
        "<GoodscCategory: 0x7ffb9c21fcf0>",
        "<GoodscCategory: 0x7ffb9c224bf0>",
        "<GoodscCategory: 0x7ffb9c224c10>",
        "<GoodscCategory: 0x7ffb9c21a0e0>",
        "<GoodscCategory: 0x7ffb9c0a0550>"
    ) as an NSUserDefaults/CFPreferences value for key sortDataArray

    经过查询,发现原因是:

    NSUserDefaults支持的数据格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL类型,

    如果想要保存其他类型或者自定义类型需要用到archiver将数据序列化为NSData类型,需要在自定义类中写encode和decode两个方法。

    GoodscCategory.h

    #import <Foundation/Foundation.h>
    
    @interface GoodscCategory : NSObject
    
    @property (nonatomic,copy) NSString *categoryID;
    @property (nonatomic,copy) NSString *categoryName;
    @property (nonatomic,retain) NSArray *subCategoryList;
    
    @end

    GoodscCategory.m

    @implementation GoodscCategory
    
    - (void)encodeWithCoder:(NSCoder *)aCoder {
        [aCoder encodeObject:_categoryID forKey:@"id"];
        [aCoder encodeObject:_categoryName forKey:@"name"];
        [aCoder encodeObject:_subCategoryList forKey:@"list"];
    }
    
    - (id)initWithCoder:(NSCoder *)aDecoder {
        if (self = [super init]) {
            self.categoryID = [aDecoder decodeObjectForKey:@"id"];
            self.categoryName = [aDecoder decodeObjectForKey:@"name"];
            self.subCategoryList = [aDecoder decodeObjectForKey:@"list"];
        }
        return self;
    }
    
    @end

    然后在存储的时候进行序列化

    - (void)saveSortArrayData:(NSArray *)array {
        
        NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:array.count];
        for (GoodscCategory *goodsObject in array) {
            NSData *goodsEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:goodsObject];
            [archiveArray addObject:goodsEncodedObject];
        }
        
        NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
        [userData setObject:archiveArray forKey:@"sortDataArray"];
    }

    取出的时候反序列化

    NSArray * dataArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"sortDataArray"];
        NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
        for (NSData *goodsData in dataArray)
        {
            GoodscCategory  *goods = [NSKeyedUnarchiver unarchiveObjectWithData:goodsData];
            [mutableArray addObject:goods];
        }

    这样,就实现了将数组array存入,使用的时候取出为数组mutableArray。

  • 相关阅读:
    HTML区块
    HTML表单
    JavaScript 表单验证
    HTML头部
    JavaScript
    设计模式—单例模式的六种写法
    new
    new
    new
    new
  • 原文地址:https://www.cnblogs.com/fanzhiying/p/5315308.html
Copyright © 2020-2023  润新知