iOS开发简单高效的数据存储

iOS开发简单高效的数据存储_第1张图片

学习交流讨论请关注新浪微博:极客James

在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题,你是用什么方法来持久保存数据的?这是在几乎每一次关于iOS技术的交流或讨论都会被提到的问题,而且大家对这个问题的热情持续高涨。本文主要从概念上把“数据存储”这个问题进行剖析,并且结合各自特点和适用场景进行全面抛析。。

一、NSUserDefaults

NSUserDefaults被设计用来存储设备和应用的配置信息,它通过一个工厂方法返回默认的、也是最常用到的实例对象。这个对象中储存了系统中用户的配置信息,开发者可以通过这个实例对象对这些已有的信息进行修改,也可以按照自己的需求创建新的配置项。
NSUserDefaults可以存储的数据类型包括:NSData、NSString、NSNumber、NSDate、NSArray、NSDictionary。如果要存储其他类型,则需要转换为前面的类型,才能用NSUserDefaults存储。具体实现为:
保存数据:

<code class="hljs objectivec has-numbering"><span class="hljs-built_in">NSUserDefaults</span> *defaults =[<span class="hljs-built_in">NSUserDefaults</span> standardUserDefaults];
<span class="hljs-built_in">NSString</span> *name =@”<span class="hljs-keyword">default</span> string“;
[defaults setObject:firstName forKey:@<span class="hljs-string">"name"</span>];
  <span class="hljs-comment">//获得UIImage实例</span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul>
<code class="hljs objectivec has-numbering"><span class="hljs-built_in">UIImage</span> *image=[[<span class="hljs-built_in">UIImage</span> alloc]initWithContentsOfFile:@<span class="hljs-string">"photo.jpg"</span>];

NSData *imageData = UIImageJPEGRepresentation(image, <span class="hljs-number">100</span>);<span class="hljs-comment">//UIImage对象转换成NSData</span>

[defaults synchronize]; <span class="hljs-comment">//用synchronize方法把数据持久化到standardUserDefaults数据库</span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>

读取数据:

<code class="hljs objectivec has-numbering"><span class="hljs-built_in">NSUserDefaults</span> *defaults =[<span class="hljs-built_in">NSUserDefaults</span> standardUserDefaults];
<span class="hljs-built_in">NSString</span> *name = [defaults objectForKey:@<span class="hljs-string">"name"</span>]; <span class="hljs-comment">//根据键值取出name</span>
NSData *imageData = [defaults dataForKey:@<span class="hljs-string">"image"</span>];
<span class="hljs-built_in">UIImage</span> *Image = [<span class="hljs-built_in">UIImage</span> imageWithData:imageData]; <span class="hljs-comment">//NSData转换为UIImage</span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li></ul>

二、归档,解归档

NSKeyedArchiver:采用归档的形式来保存数据,该数据对象需要遵守NSCoding协议,并且该对象对应的类必须提供encodeWithCoder:和initWithCoder:方法。前一个方法告诉系统怎么对对象进行编码,而后一个方法则是告诉系统怎么对对象进行解码。例如对Possession对象归档保存。
定义Possession:

<code class="hljs objectivec has-numbering"><span class="hljs-class"><span class="hljs-keyword">@interface</span> <span class="hljs-title">Possession</span>:<span class="hljs-title">NSObject</span><<span class="hljs-title">NSCoding</span>>{</span><span class="hljs-comment">//遵守NSCoding协议</span>
       <span class="hljs-built_in">NSString</span> *name;<span class="hljs-comment">//待归档类型</span>
}
<span class="hljs-class"><span class="hljs-keyword">@implementation</span> <span class="hljs-title">Possession</span></span>
-(<span class="hljs-keyword">void</span>)encodeWithCoder:(NSCoder *)aCoder{
            [aCoder encodeObject:name forKey:@<span class="hljs-string">"name"</span>];
}
-(<span class="hljs-keyword">void</span>)initWithCoder:(NSCoder *)aDecoder{
            name=[[aDeCoder decodeObjectforKey:@<span class="hljs-string">"name"</span>] retain];
}</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li></ul>

归档操作:

如果对Possession对象allPossession归档保存,只需要NSCoder子类NSKeyedArchiver的方法archiveRootObject:toFile: 即可。

<code class="hljs objectivec has-numbering"><span class="hljs-built_in">NSString</span> *path = [<span class="hljs-keyword">self</span> possessionArchivePath];
[NSKeyedArchiver archiveRootObject:allPossessions toFile: path ]</code><ul style="" class="pre-numbering"><li>1</li><li>2</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li></ul>

解压操作:

<code class="hljs fix has-numbering"><span class="hljs-attribute">同样调用NSCoder子类NSKeyedArchiver的方法unarchiveRootObject:toFile: 即可  
allPossessions </span>=<span class="hljs-string"> [[NSKeyedUnarchiver unarchiveObjectWithFile:path] retain];</span></code><ul style="" class="pre-numbering"><li>1</li><li>2</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li></ul>

缺点:归档的形式来保存数据,只能一次性归档保存以及一次性解压。所以只能针对小量数据,而且对数据操作比较笨拙,即如果想改动数据的某一小部分,还是需要解压整个数据或者归档整个数据。

三、SQLite

用SQLite存储查询需求较多的数据,是我们开发中最常见的一种方式,例如app的界面数据缓存,离线缓存等。
第一步:需要添加SQLite相关的库以及头文件:在项目文件的Build Phases下,找到Link Binary Library(ies),添加libsqlite3.0.dylib
(libsqlite3.dylib与前者的区别暂时不知,两者应该差不多);在项目文件中头文件或者源文件中添加头文件#import “/usr/include/sqlite3.h”

第二步:开始使用SQLite:

<code class="hljs objectivec has-numbering"><span class="hljs-built_in">NSArray</span> *documentsPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask , <span class="hljs-literal">YES</span>);
<span class="hljs-built_in">NSString</span> *databaseFilePath=[[documentsPaths objectAtIndex:<span class="hljs-number">0</span>] stringByAppendingPathComponent:@<span class="hljs-string">"mydb"</span>];
<span class="hljs-comment">//上面两句已经比较熟悉了吧! </span>
<span class="hljs-comment">//打开数据库</span>
<span class="hljs-keyword">if</span> (sqlite3_open([databaseFilePath UTF8String], &database)==SQLITE_OK) { 
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"sqlite dadabase is opened."</span>); 
}
<span class="hljs-keyword">else</span>{ <span class="hljs-keyword">return</span>;}<span class="hljs-comment">//打开不成功就返回</span>
在打开了数据库的前提下,如果数据库没有表,那就开始建表了哦!
<span class="hljs-keyword">char</span> *error; 
<span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *createSql=<span class="hljs-string">"create table(id integer primary key autoincrement, name text)"</span>;
<span class="hljs-keyword">if</span> (sqlite3_exec(database, createSql, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, &error)==SQLITE_OK) { 
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"create table is ok."</span>); 
}
<span class="hljs-keyword">else</span>
{
       <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"error: %s"</span>,error);
       sqlite3_free(error);<span class="hljs-comment">//每次使用完毕清空error字符串,提供给下一次使用</span>
} </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li></ul>

建表完成之后,就开始插入记录:

<code class="hljs objectivec has-numbering"><span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *insertSql=<span class="hljs-string">"insert into a person (name) values(‘gg’)"</span>; 
<span class="hljs-keyword">if</span> (sqlite3_exec(database, insertSql, <span class="hljs-literal">NULL</span>, <span class="hljs-literal">NULL</span>, &error)==SQLITE_OK) { 
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"insert operation is ok."</span>); 
}

<span class="hljs-keyword">else</span>
{
       <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"error: %s"</span>,error);
       sqlite3_free(error);<span class="hljs-comment">//每次使用完毕清空error字符串,提供给下一次使用</span>
} </code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li></ul>

下一步,查询记录:

<code class="hljs objectivec has-numbering"><span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *selectSql=<span class="hljs-string">"select id,name from a person"</span>; 
sqlite3_stmt *statement; 
<span class="hljs-keyword">if</span> (sqlite3_prepare_v2(database,selectSql, -<span class="hljs-number">1</span>, &statement, <span class="hljs-literal">nil</span>)==SQLITE_OK) { 
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"select operation is ok."</span>); 
}
<span class="hljs-keyword">else</span>
{
       <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"error: %s"</span>,error);
       sqlite3_free(error);
} 
<span class="hljs-keyword">while</span>(sqlite3_step(statement)==SQLITE_ROW) { 
<span class="hljs-keyword">int</span> _<span class="hljs-keyword">id</span>=sqlite3_column_int(statement, <span class="hljs-number">0</span>); 
<span class="hljs-built_in">NSString</span> *name=(<span class="hljs-keyword">char</span>*)sqlite3_column_text(statement, <span class="hljs-number">1</span>); 
<span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"row>>id %i, name %s"</span>,_<span class="hljs-keyword">id</span>,name); 
}
sqlite3_finalize(statement);</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li></ul>

最后,关闭数据库:

<code class="hljs scss has-numbering"><span class="hljs-function">sqlite3_close(database)</span>; </code><ul style="" class="pre-numbering"><li>1</li></ul><ul style="" class="pre-numbering"><li>1</li></ul>

注意:写入数据库,字符串可以采用char方式,而从数据库中取出char类型,当char类型有表示中文字符时,会出现乱码。这是因为数据库默认使用ascII编码方式。所以要想正确从数据库中取出中文,需要用NSString来接收从数据库取出的字符串。

四、CoreData

Core Data使用起来相对直接使用SQLite3的API而言更加的面向对象,操作过程通常分为以下几个步骤:

1.创建管理上下文

创建管理上下可以细分为:加载模型文件->指定数据存储路径->创建对应数据类型的存储->创建管理对象上下方并指定存储。

经过这几个步骤之后可以得到管理对象上下文NSManagedObjectContext,以后所有的数据操作都由此对象负责。同时如果是第一次创建上下文,Core Data会自动创建存储文件(例如这里使用SQLite3存储),并且根据模型对象创建对应的表结构。下图为第一次运行生成的数据库及相关映射文件:

为了方便后面使用,NSManagedObjectContext对象可以作为单例或静态属性来保存,下面是创建的管理对象上下文的主要代码:

<code class="hljs objectivec has-numbering">-(NSManagedObjectContext *)createDbContext{
    NSManagedObjectContext *context;
    <span class="hljs-comment">//打开模型文件,参数为nil则打开包中所有模型文件并合并成一个</span>
    NSManagedObjectModel *model=[NSManagedObjectModel mergedModelFromBundles:<span class="hljs-literal">nil</span>];
    <span class="hljs-comment">//创建解析器</span>
    NSPersistentStoreCoordinator *storeCoordinator=[[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
    <span class="hljs-comment">//创建数据库保存路径</span>
    <span class="hljs-built_in">NSString</span> *dir=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, <span class="hljs-literal">YES</span>) firstObject];
    <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"%@"</span>,dir);
    <span class="hljs-built_in">NSString</span> *path=[dir stringByAppendingPathComponent:@<span class="hljs-string">"myDatabase.db"</span>];
    <span class="hljs-built_in">NSURL</span> *url=[<span class="hljs-built_in">NSURL</span> fileURLWithPath:path];
    <span class="hljs-comment">//添加SQLite持久存储到解析器</span>
    <span class="hljs-built_in">NSError</span> *error;
    [storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:<span class="hljs-literal">nil</span> URL:url options:<span class="hljs-literal">nil</span> error:&error];
    <span class="hljs-keyword">if</span>(error){
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"数据库打开失败!错误:%@"</span>,error<span class="hljs-variable">.localizedDescription</span>);
    }<span class="hljs-keyword">else</span>{
        context=[[NSManagedObjectContext alloc]init];
        context<span class="hljs-variable">.persistentStoreCoordinator</span>=storeCoordinator;
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"数据库打开成功!"</span>);
    }
    <span class="hljs-keyword">return</span> context;
}</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li></ul>

2.查询数据
对于有条件的查询,在Core Data中是通过谓词来实现的。首先创建一个请求,然后设置请求条件,最后调用上下文执行请求的方法。

<code class="hljs objectivec has-numbering">-(<span class="hljs-keyword">void</span>)addUserWithName:(<span class="hljs-built_in">NSString</span> *)name screenName:(<span class="hljs-built_in">NSString</span> *)screenName profileImageUrl:(<span class="hljs-built_in">NSString</span> *)profileImageUrl mbtype:(<span class="hljs-built_in">NSString</span> *)mbtype city:(<span class="hljs-built_in">NSString</span> *)city{
    <span class="hljs-comment">//添加一个对象</span>
    User *us= [NSEntityDescription insertNewObjectForEntityForName:@<span class="hljs-string">"User"</span> inManagedObjectContext:<span class="hljs-keyword">self</span><span class="hljs-variable">.context</span>];
    us<span class="hljs-variable">.name</span>=name;
    us<span class="hljs-variable">.screenName</span>=screenName;
    us<span class="hljs-variable">.profileImageUrl</span>=profileImageUrl;
    us<span class="hljs-variable">.mbtype</span>=mbtype;
    us<span class="hljs-variable">.city</span>=city;
    <span class="hljs-built_in">NSError</span> *error;
    <span class="hljs-comment">//保存上下文</span>
    <span class="hljs-keyword">if</span> (![<span class="hljs-keyword">self</span><span class="hljs-variable">.context</span> save:&error]) {
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"添加过程中发生错误,错误信息:%@!"</span>,error<span class="hljs-variable">.localizedDescription</span>);
    }
}</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li></ul>

如果有多个条件,只要使用谓词组合即可,那么对于关联对象条件怎么查询呢?这里分为两种情况进行介绍:

a.查找一个对象只有唯一一个关联对象的情况,例如查找用户名为“Binger”的微博(一个微博只能属于一个用户),通过 -

(NSArray )getStatusesByUserName:(NSString )name{
NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@”Status”];
request.predicate=[NSPredicate predicateWithFormat:@”user.name=%@”,name];
NSArray *array=[self.context executeFetchRequest:request error:nil];
return array; }ata生成的SQL语句会发现其实就是把Status表和User表进行了关联查询(JOIN连接)。

b.查找一个对象有多个关联对象的情况,例如查找发送微博内容中包含“Watch”并且用户昵称为“小娜”的用户(一个用户有多条微博),此时可以充分利用谓词进行

-(NSArray )getUsersByStatusText:(NSString )text screenName:(NSString *)screenName{
NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@”Status”];
request.predicate=[NSPredicate predicateWithFormat:@”text LIKE ‘Watch‘”,text];
NSArray *statuses=[self.context executeFetchRequest:request error:nil];

NSPredicate *userPredicate= [NSPredicate predicateWithFormat:@"user.screenName=%@",screenName];
NSArray *users= [statuses filteredArrayUsingPredicate:userPredicate];
return users;

}如果单纯查找微博中包含“Watch”的用户,直接查出对应的微博,然后通过每个微博的user属性即可获得用户,此时就不用使用额外的谓词过滤条件。

3.插入数据

插入数据需要调用实体描述对象NSEntityDescription返回一个实体对象,然后设置对象属性,最后保存当前上下文即可。这里需要注意,增、删、改操作完最后必须调用管理对象上下文的保存方法,否则操作不会执行。

<code class="hljs objectivec has-numbering">-(<span class="hljs-keyword">void</span>)addUserWithName:(<span class="hljs-built_in">NSString</span> *)name screenName:(<span class="hljs-built_in">NSString</span> *)screenName profileImageUrl:(<span class="hljs-built_in">NSString</span> *)profileImageUrl mbtype:(<span class="hljs-built_in">NSString</span> *)mbtype city:(<span class="hljs-built_in">NSString</span> *)city{
    <span class="hljs-comment">//添加一个对象</span>
    User *us= [NSEntityDescription insertNewObjectForEntityForName:@<span class="hljs-string">"User"</span> inManagedObjectContext:<span class="hljs-keyword">self</span><span class="hljs-variable">.context</span>];
    us<span class="hljs-variable">.name</span>=name;
    us<span class="hljs-variable">.screenName</span>=screenName;
    us<span class="hljs-variable">.profileImageUrl</span>=profileImageUrl;
    us<span class="hljs-variable">.mbtype</span>=mbtype;
    us<span class="hljs-variable">.city</span>=city;
    <span class="hljs-built_in">NSError</span> *error;
    <span class="hljs-comment">//保存上下文</span>
    <span class="hljs-keyword">if</span> (![<span class="hljs-keyword">self</span><span class="hljs-variable">.context</span> save:&error]) {
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"添加过程中发生错误,错误信息:%@!"</span>,error<span class="hljs-variable">.localizedDescription</span>);
    }
}</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li></ul>

4.删除数据

删除数据可以直接调用管理对象上下文的deleteObject方法,删除完保存上下文即可。注意,删除数据前必须先查询到对应对象。

<code class="hljs objectivec has-numbering">-(<span class="hljs-keyword">void</span>)removeUser:(User *)user{
    [<span class="hljs-keyword">self</span><span class="hljs-variable">.context</span> deleteObject:user];
    <span class="hljs-built_in">NSError</span> *error;
    <span class="hljs-keyword">if</span> (![<span class="hljs-keyword">self</span><span class="hljs-variable">.context</span> save:&error]) {
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"删除过程中发生错误,错误信息:%@!"</span>,error<span class="hljs-variable">.localizedDescription</span>);
    }
}</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li></ul>

5.修改数据

修改数据首先也是取出对应的实体对象,然后通过修改对象的属性,最后保存上下文。

<code class="hljs objectivec has-numbering">-(<span class="hljs-keyword">void</span>)modifyUserWithName:(<span class="hljs-built_in">NSString</span> *)name screenName:(<span class="hljs-built_in">NSString</span> *)screenName profileImageUrl:(<span class="hljs-built_in">NSString</span> *)profileImageUrl mbtype:(<span class="hljs-built_in">NSString</span> *)mbtype city:(<span class="hljs-built_in">NSString</span> *)city{
    User *us=[<span class="hljs-keyword">self</span> getUserByName:name];
    us<span class="hljs-variable">.screenName</span>=screenName;
    us<span class="hljs-variable">.profileImageUrl</span>=profileImageUrl;
    us<span class="hljs-variable">.mbtype</span>=mbtype;
    us<span class="hljs-variable">.city</span>=city;
    <span class="hljs-built_in">NSError</span> *error;
    <span class="hljs-keyword">if</span> (![<span class="hljs-keyword">self</span><span class="hljs-variable">.context</span> save:&error]) {
        <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"修改过程中发生错误,错误信息:%@"</span>,error<span class="hljs-variable">.localizedDescription</span>);
    }
}</code><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li></ul><ul style="" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li></ul>

调试
虽然Core Data(如果使用SQLite数据库)操作最终转换为SQL操作,但是调试起来却不想操作SQL那么方便。特别是对于初学者而言经常出现查询报错的问题,如果能看到最终生成的SQL语句自然对于调试很有帮助。事实上在Xcode中是支持Core Data调试的,具体操作:Product-Scheme-Edit Scheme-Run-Arguments中依次添加两个参数(注意参数顺序不能错):-com.apple.CoreData.SQLDebug、1。然后在运行程序过程中如果操作了数据库就会将SQL语句打印在输出面板。

你可能感兴趣的:(ios,深度学习,数据储存)