美文网首页
数据存储

数据存储

作者: 石玉龙 | 来源:发表于2016-11-06 17:27 被阅读8次

    1> XML属性列表归档:.plist 

    2> 偏好设置:NSUserDefault

    3> 归档:NSKeydeArchiver

    4> 关系型数据库:Sqlite 3

    5> 对象型的数据库:Core Data

    沙盒:文件系统目录,iOS应用程序都是独立的沙盒;

    Documents: 如:数据库文件放置这里;

    Library: Caches, Preferences 如:图片等缓冲文件放置这里,会自动删除掉的空间;

    tmp:

    推介工具软件:SimPholders

    - (void)initWithOther

    {

    // 根目录

    NSString *home = NSHomeDirectory();

    NSLog(@"home : %@", home);

    // 获取Document,创建一个bank.plist文件

    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

    NSString *filePath = [document stringByAppendingPathComponent:@"bank.plist"];

    NSLog(@"filePath : %@", filePath);

    // 字典写入

    NSDictionary *dic = @{@"name" : @"yulong", @"count" : @"6", @"id" : @"123456", @"type" : @"农行"};

    [dic writeToFile:filePath atomically:YES];

    // 读文件

    NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:filePath];

    NSLog(@"readDic : %@", readDic);

    }

    - (void)writeData

    {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:@"yulong" forKey:@"name"];

    [defaults setInteger:1 forKey:@"money"];

    [defaults setDouble:1.78 forKey:@"height"];

    [defaults synchronize];

    }

    - (void)readData

    {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *name = [defaults objectForKey:@"name"];

    NSUInteger money = [defaults integerForKey:@"money"];

    double height = [defaults doubleForKey:@"height"];

    NSLog(@"name : %@, money : %lu, height : %f", name, (unsigned long)money, height);

    }

    偏好设置:保存应用程序的配置信息;

    - (void)writeArchive

    {

    Student *student = [[Student alloc] init];

    student.name = @"yulong";

    student.age = 8;

    student.className = @"小班一班";

    student.classID = @"1234560798";

    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSString *filePath = [document stringByAppendingPathComponent:@"student.txt"];

    NSLog(@"filePath : %@", filePath);

    [NSKeyedArchiver archiveRootObject:student toFile:filePath];

    }

    - (void)readArchive

    {

    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSString *filePath = [document stringByAppendingPathComponent:@"student.txt"];

    NSLog(@"filePath : %@", filePath);

    Student *student = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

    NSLog(@"%@ %lu %@ %@", student.name, (unsigned long)student.age, student.className, student.classID);

    }

    自定义对象保存到文件中,必须实现<NSCoding>协议;

    - (instancetype)initWithCoder:(NSCoder *)aDecoder

    {

    NSLog(@"%s", __func__);

    self = [super init];

    if (self)

    {

    _name = [aDecoder decodeObjectForKey:@"name"];

    _age = [aDecoder decodeIntegerForKey:@"age"];

    _className = [aDecoder decodeObjectForKey:@"className"];

    _classID = [aDecoder decodeObjectForKey:@"classID"];

    }

    return self;

    }

    - (void)encodeWithCoder:(NSCoder *)aCoder

    {

    NSLog(@"%s", __func__);

    [aCoder encodeObject:_name forKey:@"name"];

    [aCoder encodeInteger:_age forKey:@"age"];

    [aCoder encodeObject:_className forKey:@"className"];

    [aCoder encodeObject:_classID forKey:@"classID"];

    }

    相关文章

      网友评论

          本文标题:数据存储

          本文链接:https://www.haomeiwen.com/subject/qkknuttx.html