服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - 详解iOS应用开发中Core Data数据存储的使用

详解iOS应用开发中Core Data数据存储的使用

2021-01-07 15:27苹果吧 IOS

这篇文章主要介绍了iOS应用开发中Core Data数据存储的使用,Core Data可以看作是一个内嵌型数据库SQLite的iOS专用版本,需要的朋友可以参考下

1.如果想创建一个带有coredata的程序,要在项目初始化的时候勾选中
详解iOS应用开发中Core Data数据存储的使用 
2.创建完成之后,会发现在appdelegate里多出了几个属性,和2个方法

复制代码 代码如下:

<span style="font-size:18px;"> 
 
@property (readonly, strong, nonatomic) nsmanagedobjectcontext *managedobjectcontext; 
@property (readonly, strong, nonatomic) nsmanagedobjectmodel *managedobjectmodel; 
@property (readonly, strong, nonatomic) nspersistentstorecoordinator *persistentstorecoordinator; 
 
- (void)savecontext; 
- (nsurl *)applicationdocumentsdirectory;</span> 


core data数据持久化是对sqlite的一个升级,它是ios集成的,在说core data之前,我们先说说在coredata中使用的几个类。

 

(1)nsmanagedobjectmodel(被管理的对象模型)

相当于实体,不过它包含 了实体间的关系

(2)nsmanagedobjectcontext(被管理的对象上下文)

操作实际内容

作用:插入数据  查询  更新  删除

(3)nspersistentstorecoordinator(持久化存储助理)

相当于数据库的连接器

(4)nsfetchrequest(获取数据的请求)

相当于查询语句

(5)nspredicate(相当于查询条件)

(6)nsentitydescription(实体结构)

(7)后缀名为.xcdatamodel的包

里面的.xcdatamodel文件,用数据模型编辑器编辑

编译后为.momd或.mom文件,这就是为什么文件中没有这个东西,而我们的程序中用到这个东西而不会报错的原因。


3.如果想创建一个实体对象的话,需要点击.xcdatamodel,add entity,添加想要的字段

详解iOS应用开发中Core Data数据存储的使用详解iOS应用开发中Core Data数据存储的使用

4.生成对象文件,command+n,然后选中coredata里的nsmanagerobjectsubclass进行关联,选中实体创建

详解iOS应用开发中Core Data数据存储的使用

5.添加数据

复制代码 代码如下:

person *newperson = [nsentitydescription insertnewobjectforentityforname:@"person" inmanagedobjectcontext:self.managedobjectcontext]; 
     
    if (newperson == nil){ 
        nslog(@"failed to create the new person."); 
        return no; 
    } 
     
    newperson.firstname = paramfirstname; 
    newperson.lastname = paramlastname; 
    newperson.age = [nsnumber numberwithunsignedinteger:paramage]; 
    nserror *savingerror = nil; 
     
    if ([self.managedobjectcontext save:&savingerror]){ 
        return yes; 
    } else { 
        nslog(@"failed to save the new person. error = %@", savingerror); 
    } 

 

nsentitydescription(实体结构)相当于表格结构

6.取出数据查询

复制代码 代码如下:

/* create the fetch request first */ 
    nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; 
    /* here is the entity whose contents we want to read */ 
    nsentitydescription *entity = 
    [nsentitydescription 
     entityforname:@"person" 
     inmanagedobjectcontext:self.managedobjectcontext]; 
    /* tell the request that we want to read the
     contents of the person entity */ 
    [fetchrequest setentity:entity]; 
    nserror *requesterror = nil; 
    /* and execute the fetch request on the context */ 
    nsarray *persons = 
    [self.managedobjectcontext executefetchrequest:fetchrequest 
                                             error:&requesterror]; 
    /* make sure we get the array */ 
    if ([persons count] > 0){ 
        /* go through the persons array one by one */ 
        nsuinteger counter = 1; 
        for (person *thisperson in persons){ 
            nslog(@"person %lu first name = %@", 
                  (unsigned long)counter,  
                  thisperson.firstname);  
            nslog(@"person %lu last name = %@",  
                  (unsigned long)counter,  
                  thisperson.lastname); 
            nslog(@"person %lu age = %ld", 
                  (unsigned long)counter, 
                  (unsigned long)[thisperson.age unsignedintegervalue]); 
            counter++; 
        } 
    } else { 
        nslog(@"could not find any person entities in the context.");  
    } 

 

7.删除数据

复制代码 代码如下:

/* create the fetch request first */ 
    nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; 
    /* here is the entity whose contents we want to read */ 
    nsentitydescription *entity = 
    [nsentitydescription 
     entityforname:@"person" 
     inmanagedobjectcontext:self.managedobjectcontext]; 
    /* tell the request that we want to read the
     contents of the person entity */ 
    [fetchrequest setentity:entity]; 
    nserror *requesterror = nil; 
    /* and execute the fetch request on the context */ 
    nsarray *persons = 
    [self.managedobjectcontext executefetchrequest:fetchrequest 
                                             error:&requesterror]; 
    if ([persons count] > 0){ 
        /* delete the last person in the array */ 
        person *lastperson = [persons lastobject]; 
        [self.managedobjectcontext deleteobject:lastperson]; 
        nserror *savingerror = nil; 
        if ([self.managedobjectcontext save:&savingerror]){ 
            nslog(@"successfully deleted the last person in the array."); 
        } else { 
            nslog(@"failed to delete the last person in the array."); 
        } 
    } else {  
        nslog(@"could not find any person entities in the context.");  
    }  

 

8.排序

复制代码 代码如下:

<pre code_snippet_id="243955" snippet_file_name="blog_20140319_5_4289257" name="code" class="objc">nssortdescriptor *agesort =  
[[nssortdescriptor alloc] initwithkey:@"age"  
ascending:yes];  
nssortdescriptor *firstnamesort =  
[[nssortdescriptor alloc] initwithkey:@"firstname"  
ascending:yes];  
nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:  
agesort,  
firstnamesort, nil nil];  
fetchrequest.sortdescriptors = sortdescriptors; </pre><p></p> 
<pre></pre> 
<p></p> 
<p style="background-color:rgb(255,255,255); margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,'ms song',arial,helvetica,sans-serif; line-height:19.09090805053711px"> 
<span style="background-color:rgb(255,255,255)"><span style="font-size:18px"><br> 
</span></span></p> 
<span style="background-color:rgb(255,255,255)">注意</span><span style="font-size:18px; background-color:rgb(255,255,255)">ascending:yes 属性决定排序顺序</span><span style="background-color:rgb(255,255,255)"><span style="font-size:18px"><br> 
<br> 
<br> 
</span></span><br> 

延伸 · 阅读

精彩推荐
  • IOSiOS中滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和Masonry简单使用)

    iOS中滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和

    这篇文章主要介绍了iOS中滑动控制屏幕亮度和系统音量(附加AVAudioPlayer基本用法和Masonry简单使用)的相关资料,需要的朋友可以参考下...

    CodingFire13652021-02-26
  • IOSiOS开发技巧之状态栏字体颜色的设置方法

    iOS开发技巧之状态栏字体颜色的设置方法

    有时候我们需要根据不同的背景修改状态栏字体的颜色,下面这篇文章主要给大家介绍了关于iOS开发技巧之状态栏字体颜色的设置方法,文中通过示例代码...

    梦想家-mxj8922021-05-10
  • IOSiOS中MD5加密算法的介绍和使用

    iOS中MD5加密算法的介绍和使用

    MD5加密是最常用的加密方法之一,是从一段字符串中通过相应特征生成一段32位的数字字母混合码。对输入信息生成唯一的128位散列值(32个字符)。这篇文...

    LYSNote5432021-02-04
  • IOSiOS中UILabel实现长按复制功能实例代码

    iOS中UILabel实现长按复制功能实例代码

    在iOS开发过程中,有时候会用到UILabel展示的内容,那么就设计到点击UILabel复制它上面展示的内容的功能,也就是Label长按复制功能,下面这篇文章主要给大...

    devilx12792021-04-02
  • IOSiOS开发之视图切换

    iOS开发之视图切换

    在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单。在iOS开发中常用的视图切换有三种,今天我们将...

    执着丶执念5272021-01-16
  • IOSiOS自定义UICollectionViewFlowLayout实现图片浏览效果

    iOS自定义UICollectionViewFlowLayout实现图片浏览效果

    这篇文章主要介绍了iOS自定义UICollectionViewFlowLayout实现图片浏览效果的相关资料,需要的朋友可以参考下...

    jiangamh8882021-01-11
  • IOSiOS实现控制屏幕常亮不变暗的方法示例

    iOS实现控制屏幕常亮不变暗的方法示例

    最近在工作中遇到了要将iOS屏幕保持常亮的需求,所以下面这篇文章主要给大家介绍了关于利用iOS如何实现控制屏幕常亮不变暗的方法,文中给出了详细的...

    随风13332021-04-02
  • IOS详解iOS中多个网络请求的同步问题总结

    详解iOS中多个网络请求的同步问题总结

    这篇文章主要介绍了详解iOS中多个网络请求的同步问题总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    liang199111302021-03-15