iOS之对象保存

        iOS中对于数据的存储通常使用sqlite或者coredata,也有部分使用文件存储的方式。iOS中的NSArray、NSString、NSDictionary、NSData等常规的数据对象都可以使用writeToFile:atomically:方法(注:这里NSArray中的对象必须是些iOS中的常规对象,如果是自定义的且有嵌套对象的,调用该方法会失败),因为实际操作中自定义对象的多样化,如果要实现文件保存通常要进行数据转换操作,然后再使用writeToFile:atomically:方法去写文件,读取的时候再按对象存储后的方式解析。为了实现通用性,本文使用<objc/runtime.h>中的方法对对象动态存储及解析。

以如下对象为例:

1、对象一嵌套ImageInfo对象

@interface HotRecommend : NSObject<NSCopying>

@property(nonatomic,retain) NSString *startTime;

@property(nonatomic,retain) NSString *endTime;

@property(nonatomic,retain) ImageInfo *img;

2、对象二嵌套CDCPoint对象

@interface ImageInfo : NSObject

@property (retain) NSString *Id;

@property (retain) NSString *Name;

@property (retain) NSString *Url;

@property (retain) NSNumber *Width;

@property (retain) NSNumber *Height;

@property (retain) NSString *SourceType;

@property(retain) CDCPoint *point;

3、对象三为常用数据类型

@interface CDCPoint : NSObject

@property(nonatomic,assign) float pointX;

@property(nonatomic,assign) float pointY;


对于上述包含多个 HotRecommend 对象的数组存储,调用writeToFile:atomically:就无法实现目的,具体思路可以将HotRecommend转化为字典对象,或json字符串,最后再调用writeToFile:atomically:去存储

方法如下(该方法可能未考虑到所有的对象转换情况,具体情况读者可以根据思路自行添加):

//对象转换为字典,调用该方法将自定义的对象转换为字典对象,(思路:通过遍历对象中的属性及其值进行转换)

+ (NSDictionary *)objToNSDictionary:(id)object

{

    if ([object isKindOfClass:[NSDictionary class]]) {

        return object;

    }else{

        NSMutableDictionary *dicRet = [[NSMutableDictionary alloc] initWithCapacity:0];

        unsigned int count = 0;

        objc_property_t *properties = class_copyPropertyList([object class], &count);

        for( int i = 0; i < count; i++ )

        {

            objc_property_t property = properties[i];

            const char *name = property_getName(property);

     //获取对象属性名称及值

            NSString *propertyName = [NSString stringWithUTF8String:name];

            id value = [object valueForKey:propertyName];

            if (value) {

                if( ![value isKindOfClass:[NSArray class]]

                   && ![value isKindOfClass:[NSString class]]

                   && ![value isKindOfClass:[NSDictionary class]]

                   && ![value isKindOfClass:[NSNumber class]])

                {

                    NSDictionary *dicValue =  [self objToNSDictionary:value];

                    [dicRet setObject:dicValue forKey:propertyName];

                }else{

                    [dicRet setObject:value forKey:propertyName];

                }

            }else{

                [dicRet setObject:@"" forKey:propertyName];

            }

        }

        return dicRet;


    }

}

//字典转化为制定名称的对象,className为你要转换成的对象名称,(请事先定义好类对象)

+(id)dicToObj:(NSDictionary *)dic withClassName:(NSString *)className;

{

    Class clas = NSClassFromString(className);

    id retObj = [[clas alloc] init];

    unsigned int ivarsCnt = 0;

    Ivar *ivars = class_copyIvarList(clas, &ivarsCnt);

    for (const Ivar *p = ivars; p < ivars + ivarsCnt; ++p)

    {

        Ivar const ivar = *p;

        // 获取变量名

        NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];

        // 取得变量类型

        const char *type = ivar_getTypeEncoding(ivar);

        // 获取变量值

        id value = [dic valueForKey:key];

        NSString *str = [NSString stringWithUTF8String:type];

        //获取嵌套自定义类名称

        NSString *strTMp = [str stringByReplacingOccurrencesOfString:@"\"" withString:@""];

        strTMp = [strTMp stringByReplacingOccurrencesOfString:@"@" withString:@""];

        Class tmpClass = NSClassFromString(strTMp);

        if (tmpClass != [NSString class] && tmpClass != [NSDictionary class] && tmpClass != [NSNumber class] && tmpClass != [NSArray class] && tmpClass != nil) {

            id tmpValue = [self dicToObj:value withClassName:strTMp];

            [retObj setValue:tmpValue forKey:key];

        }else{

            [retObj setValue:value forKey:key];

        }

       

    }

    return retObj;

}

对于json情况,思路一样,读者可自行完成。随便写写,非喜勿喷~如果有更好的思路或方法,欢迎留言讨论!谢谢!






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值