zoukankan      html  css  js  c++  java
  • iphone 文件保存策略

    iphone 文件保存策略

     
    文件保存策略:

    一般有三中方法:1,属性列表,2,对象归档,3,iphone的嵌入式数据库库(sqLite3)

    1,属性列表

    存储文件:

    //获取文档目录,NSDocumentDirectory表示我们查找Documents目录的路径,NSUserDomainMask表示我们的搜索范围只能在我们的应用程序沙盒当中,

    NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectoryNSUserDomainMask,YES);

    //每个应用程序只有一个路径,所以说我们可以从数组的0位置取得路径

     NSString *documentDirectory=[path objectAtIndex:0];

    //获得的路径当中生成一个theFile.txt文件,filename将包含theFile.txt的完整路径

    NSString *filename=[documentDirectorystringByAppendingPathComponent:@“theFile.txt"];

     NSMutableArray *array=[[NSMutableArray allocinit];

     [array addObject:@"data1"];

     [array addObject:@"data2"];

     [array addObject:@"data3"];

     [array writeToFile:documentDirectory atomically:YES];

    如果想获取临时文件的目录可以使用下面的语句:

    NSString *tempPath=NSTemporaryDirectory();

     

    读取文件:

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSString *documentdirectory=[path objectAtIndex:0];

     if ([[NSFileManager defaultManagerfileExistsAtPathdocumentdirectory]) {

     NSArray *array=[[NSArray alloc]initWithContentsOfFile:filePath];

     field1.text=[array objectAtIndex:0];

     field2.text=[array objectAtIndex:1];

     field3.text=[array objectAtIndex:2];

     field4.text=[array objectAtIndex:3];

    属性列表的问题是无法将自定义对象序列化到属性列表中,也不能使用CocoaTouch交付,无法使用NSURL,UIImage和UIColor等。

    2,对象归档

    符合NSCoding,NSCoping,NSCopying是必须要符合的,NSCopying不是必须的,单符合NSCopying可以使你在使用数据模型时具备更多的灵活性。

    NSCoding中申明了两个方法,这两个方法都是必须的,一个是将对象编码到归档中,另一个是通过对归档编码来创建一个新对象,两个方法都传递一个NSCoder实例,

    -(void) encodeWithCoder:(NSCoder *)aCoder{

     

     

     

    }

    -(id)initWithCoder:(NSCoder *)aDecoder{

     if (self=[super init]) {

     

     

     }

     

     return self;

    }

    //实现NSCopying

    -(id) copyWithZone:(NSZone *)zone{

     

     return d;

     

    }

      对数据进行归档

    //创建一个NSMutableData实例,用来包含编码的数据

    NSMutableData *data=[NSMutableData alloc] init];

    //创建一个归档对象,该归档对象将归档的数据储存在data里

    NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:data]

    [archiver encodeObject:myobject forKey:@"keyvalueString"];

    //完成归档

    [archiver finishEncoding];

    //将data里面归档的数据写入文件 ,写入文件成功为Success,失败时为No;

    Bool success=[data writetoFile:@"/path/to/archive" atomically:YES];

    [archiver release];

    [data release];

    对数据对象取消归档

    //从归档文件创建一个NSData实例

    NSData *data=[[NSData alloc] initWithContentsOfFile:path];

    //创建一个NSKeyedUnarchiver,对数据进行解码

    NSIKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:data];

    //通过key值解码对象

    self.object=[unarchiver decodeObjectForKey:@"keyValueString"];

    [unarchiver finishDecoding];

    [unarchiver release];

    [data release];

    3.SQLite3

    创建或打开数据库

    sqlite3 *database;

    //如果result等于常量SQLITE_OK,则表示已经打开(数据库文件路径必须作为C字符串处理)

    int result=sqlite3_open("/path/database/file",&database);

    char *errorMsg;

    const char *createSQL-"CREATE TABLE IF NOT EXITS PEOPLE(ID INTEGER PRIMARY KEY AUTOINCREMENT,FIELD_DATA TEXT)";

    int result=sqlite3_exec(database,createSQL,NULL,NULL,&errorMsg);

    关闭数据库

    sqlite3_close(database);

  • 相关阅读:
    android29
    android28
    android27
    android26
    Dynamics CRM2011 MspInstallAction failed when installing an Update Rollup
    Dynamics CRM Import Solution Attribute Display Name description is null or empty
    The service cannot be activated because it does not support ASP.NET compatibility
    IIS部署WCF报 无法读取配置节“protocolMapping”,因为它缺少节声明
    Unable to access the IIS metabase.You do not have sufficient privilege
    LM算法与非线性最小二乘问题
  • 原文地址:https://www.cnblogs.com/neworiginou/p/2307437.html
Copyright © 2011-2022 走看看