• 使用NSUserDefaults保存自定义对象(转)


    转自http://zani.iteye.com/blog/1431239

    .h文件

    #import <Foundation/Foundation.h>
    
    @interface MyObject : NSObject
    {
        NSNumber* lowValue;
        NSNumber* highValue;
        
        NSString* titleString;
    }
    @property(nonatomic, retain)NSNumber* lowValue;
    @property(nonatomic, retain)NSNumber* highValue;
    
    @property(nonatomic, retain)NSString* titleString;
    @end

    .m

    #import "MyObject.h"
    
    @implementation MyObject
    @synthesize lowValue, highValue, titleString;
    - (void)encodeWithCoder:(NSCoder *)encoder 
    {
        [encoder encodeObject:self.lowValue forKey:@"lowValue"];
        [encoder encodeObject:self.highValue forKey:@"highValue"];
        [encoder encodeObject:self.titleString forKey:@"titleString"];
    }
    - (id)initWithCoder:(NSCoder *)decoder 
    {
        if(self = [super init])
        {
            self.lowValue = [decoder decodeObjectForKey:@"lowValue"];
            self.highValue = [decoder decodeObjectForKey:@"highValue"];
            self.titleString = [decoder decodeObjectForKey:@"titleString"];
        }
        return  self;
    }
    
    @end

    保存单个MyObject方法:

    - (void)saveCustomObject:(MyObject *)obj 
    {
        NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:myEncodedObject forKey:@"myEncodedObjectKey"];
    }
    - (MyObject *)loadCustomObjectWithKey:(NSString *)key 
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *myEncodedObject = [defaults objectForKey:key];
        MyObject *obj = (MyObject *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
        return obj;
    }
    
    保存:
            MyObject* testObj = [[MyObject alloc] init];
            testObj.lowValue  =[NSNumber  numberWithFloat:122.2 ];
            testObj.highValue = [NSNumber numberWithFloat:19888 ];
            testObj.titleString = @“baoyu”;
    
    读取:
        MyObject* obj = [self loadCustomObjectWithKey:@"myEncodedObjectKey"];
        NSLog(@"%f, %f, %@", [obj.lowValue floatValue], [obj.highValue floatValue], obj.titleString);

    保存多个MyObject方法:

    保存:
        NSMutableArray* array = [[NSMutableArray alloc] init];
        for(int i=0; i<3; i++)
        {
            MyObject* testObj = [[MyObject alloc] init];
            testObj.lowValue  =[NSNumber  numberWithFloat:122.2+i ];
            testObj.highValue = [NSNumber numberWithFloat:19888+i ];
            testObj.titleString = [NSString stringWithFormat:@"BAOYU%d", i];
            
            [array addObject:testObj];
    
        }
     [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:array] forKey:@"myarray"];
    
    
    读取:
            NSData* data  = [[NSUserDefaults standardUserDefaults] objectForKey:@"myarray"];
        
    
        NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        for(MyObject* obj in oldSavedArray)
        {
            NSLog(@"%f, %f, %@", [obj.lowValue floatValue], [obj.highValue floatValue], obj.titleString);
        }
  • 相关阅读:
    Dynamic CRM 2013学习笔记(四)单据编号及插件批量注册工具
    Dynamic CRM 2013学习笔记(三)快速创建实体 EntityCreater
    Dynamic CRM 2013学习笔记(二)插件基本用法及调试
    Dynamic CRM 2013学习笔记(一)插件输入实体参数解析
    MVC 4 图片的上传及显示
    CRM 2013 批量更新two options的缺省值
    ASP.NET MVC & Web API Brief Introduction
    Mac下访问windows的共享文件夹
    解决Android编译so库出错问题
    解决Xcode 6 编译Cocos2d-x iOS项目失败
  • 原文地址:https://www.cnblogs.com/xiaobaizhu/p/3448933.html
Copyright © 2020-2023  润新知