iOS 本地数据存储(一)


本地数据存储之UserDefaults


UserDefaults支持的基本数据类型包含:

NSDataNSStringNSNumberNSDateNSArray, or NSDictionary


对于自定义的类,如果直接存储会出现如下错误:

Property list invalid for format (property lists cannot contain objects of type ‘CFType’)


如果想存储自定义的类,需要对其进行编码即转换为基本数据类型。


类定义,以及转换为NSDictionary类型:

@interface XYZToDoItem : NSObject

@property NSString *itemName;
@property BOOL completed;
//@property (readonly) NSDate *creationDate;
@property NSDate *creationDate;

- (NSDictionary *) encodedItem;

@end

</pre><pre name="code" class="cpp">@implementation XYZToDoItem

#pragma Used for encoded objects to be able to save to UserDeafaults
- (NSDictionary *) encodedItem
{
    return [NSDictionary dictionaryWithObjectsAndKeys:self.itemName, @"itemName",
            [NSNumber numberWithBool:self.completed], @"completed",
            self.creationDate , @"creationDate", nil];
}

@end


调用的地方,存储如UserDefault里,并且读取转换为相应的类:
- (void) saveUserDefaultsOwn
{
    XYZToDoItem *item1 = [[XYZToDoItem alloc] init];
    item1.itemName = @"Buy milk";
    //[self.toDoItems addObject:item1];
    XYZToDoItem *item2 = [[XYZToDoItem alloc] init];
    item2.itemName = @"Buy eggs";
    //[self.toDoItems addObject:item2];
    XYZToDoItem *item3 = [[XYZToDoItem alloc] init];
    item3.itemName = @"Read a book";
    //[self.toDoItems addObject:item3];
    
    
    NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
    [userdefaults setObject:[item1 encodedItem] forKey:@"item1"];
    [userdefaults setObject:[item2 encodedItem] forKey:@"item2"];
    [userdefaults setObject:[item3 encodedItem]forKey:@"item3"];
    
    [userdefaults synchronize];
}

- (void) readUserDeafultsOwn
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic1 = [[NSDictionary alloc] init];
    
    
    dic1 = [userDefaults objectForKey:@"item1"];
    
    XYZToDoItem *item1 = [[XYZToDoItem alloc] init];
    item1.itemName = [dic1 objectForKey:@"itemName"];
    item1.completed = [dic1 objectForKey:@"completed"];
    item1.creationDate = [dic1 objectForKey:@"creationDate"]; //initial it is readonly, not right

    dic1 = [userDefaults objectForKey:@"item2"];
    XYZToDoItem *item2 = [[XYZToDoItem alloc] init];
    item2.itemName = [dic1 objectForKey:@"itemName"];
    item2.completed = [dic1 objectForKey:@"completed"];
    item2.creationDate = [dic1 objectForKey:@"creationDate"]; //initial it is readonly, not right
    
    dic1 = [userDefaults objectForKey:@"item2"];
    XYZToDoItem *item3 = [[XYZToDoItem alloc] init];
    item3.itemName = [dic1 objectForKey:@"itemName"];
    item3.completed = [dic1 objectForKey:@"completed"];
    item3.creationDate = [dic1 objectForKey:@"creationDate"]; //initial it is readonly, not right
    
    //XYZToDoItem *item2 = [userDefaults objectForKey:@"item2"];
    //XYZToDoItem *item3 = [userDefaults objectForKey:@"item3"];
    
    [self.toDoItems addObject:item1];
    [self.toDoItems addObject:item2];
    [self.toDoItems addObject:item3];
}


综上为简单的对复杂的类进行简单封装并且读取的例子。



[参考:http://82apps.tumblr.com/post/11989743237/adding-turn-based-multiplayer-in-ios-5-part-3]

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。