博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用NSUserDefaults保存自定义对象(转)
阅读量:6292 次
发布时间:2019-06-22

本文共 2817 字,大约阅读时间需要 9 分钟。

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

.h文件

#import 
@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);    }

 

转载于:https://www.cnblogs.com/xiaobaizhu/p/3448933.html

你可能感兴趣的文章
Docker之功能汇总
查看>>
!!a标签和button按钮只允许点击一次,防止重复提交
查看>>
(轉貼) Eclipse + CDT + MinGW 安裝方法 (C/C++) (gcc) (g++) (OS) (Windows)
查看>>
还原数据库
查看>>
作业调度框架 Quartz.NET 2.0 beta 发布
查看>>
mysql性能的检查和调优方法
查看>>
项目管理中的导向性
查看>>
Android WebView 学习
查看>>
(转)从给定的文本中,查找其中最长的重复子字符串的问题
查看>>
HDU 2159
查看>>
spring batch中用到的表
查看>>
资源文件夹res/raw和assets的使用
查看>>
UINode扩展
查看>>
LINUX常用命令
查看>>
百度云盘demo
查看>>
概率论与数理统计习题
查看>>
初学structs2,简单配置
查看>>
Laravel5.0学习--01 入门
查看>>
时间戳解读
查看>>
sbin/hadoop-daemon.sh: line 165: /tmp/hadoop-hxsyl-journalnode.pid: Permission denied
查看>>