coreData初步认识

作者: 和珏猫 | 来源:发表于2016-02-28 16:57 被阅读18839次

    coreData初步认识

    根据这片文档可以自己初步建立一个coreData,进行简单的数据的增删改查。

    1、新建工程,记得勾选Use Core Data

    2、建立好以后可以看到xxx.xcdatamodeld,在这里可以添加实体和实体的属性。需要注意的是:实体名字必须以大写开头。

    3、然后新建一个file,记得是NSManagedObject cubclass

    4、勾选自己建立的工程

    5、勾选建立的实体

    6、next以后我们就可以看到建立好的实体是有4个文件,如图一

    这里需要注意的是,xcode7以后建立的都是4个,而7以前的是两个,如图二

    解释如下:So as you can see now all properties are in a separate file with category (CoreDataProperties). Later if you generate NSManagedObject subclass for the same model Xcode 7 will regenarete only 2 files with category (DBUser+CoreDataProperties.h and DBUser+CoreDataProperties.m) to update all properties from your model but it will not make any changes to 2 other files (DBUser.h and DBUser.m) so you can use these 2 files to add there some custom methods or properties etc.

    In previous version Xcode generated always only 2 files (DBUser.h and DBUser.m) and it put properties there so you could not easily modify these files because your custom implementation was deleted everytime you regenerated your subclasses. Therefore it was a common practice to manually create a category and put your methods in your category which was oposite to what we can see in Xcode 7. That however had many disadvantages because we had to use a category for implementation of our methods which does not allow to do certain things and now we can easily modify the main interface and implementation files which allows us to do anything with it. Hurray!

    图一

    图二

    7、

    8、增删改查(其实顺序应该是增、查、删或者改)

    查询结果

    删除后再进行查,查询结果:

    改完后的结果:

    至此,完成。

    这里知识coredata的初步认识,具体的增删改查还需要和自己的项目结合。

    相关文章

      网友评论

      • 清蒸鱼跃龙门:NSManagedObject cubclass,,, 我的xcode没有这个啊
      • 经文纬武:一运行直接崩溃 提示 reason: '-[AppDelegate managedObjectContext]: unrecognized selector sent to instance 0x600000044b30'
      • 孙好运:有项目吗??
      • 简书VIP:改这样写好点
        let batch = NSBatchUpdateRequest(entityName: entityName)
        batch.propertiesToUpdate = ["age" : 18]
        batch.predicate = NSPredicate(format: "age < %@", 18 as NSNumber)
        batch.resultType = .UpdatedObjectsCountResultType


        do{
        let result = try managedObjectContext.executeRequest(batch)
        if let theResult = result as? NSBatchUpdateResult{
        if let numberOfAffectedPersons = theResult.result as? Int{
        print("Number of people who were previously younger than " +
        "18 years old and whose age is now set to " +
        "18 is \(numberOfAffectedPersons)")
        }
        }
        } catch let error as NSError{
        print("Could not perform batch request. Error = \(error)")
        }
      • 9ff81828ec00:楼主写的好~ 我有个问题想请教哈~
        如果我只想改name 里的某一部分: 比如把花花改成嘿嘿, 请问应该怎么写呢?
        另外 NSPredicate的 predicateWithFormat 必须写成@"name=%@"这样吗?
        谢谢楼主回复~
        和珏猫:@小样和牛牛 不是吧,我的意思是可以是一句话的一半,比如“中午吃的蛋炒饭”,那我是不是可以只用查“的蛋炒饭”就能查到,但是不能像你说的“中午吃饭”
        9ff81828ec00:@和珏猫 谢谢楼主回答, 如果我写搜名字里有花的, 是写成 name=花 吗?
        和珏猫:@小样和牛牛 查找的话,应该是按照固定格式查找,比方我们的格式是“name=花花8”,那么就应该按照这种格式查找,当然,这个只是我们的例子,如果实际应用中,需要改成实际应用中的语句。我觉得,在例子的语句中,甚至查找“=花花8”,就像我们平时查找似的,但是没有亲测,不知道是否可行。
      • 885389eb08b2:支持下,不过我想知道如果同时删除,两个或者两个以上的数据该用什么方法,我看了那个后面可以添加,但是不知道怎么添加
      • 共由石石石:棒
        和珏猫:@HLZNJ 谢谢
      • toplee:你好?能留个扣扣吗?有点coredata的问题想要请教请教你?1713521474
      • dapeng199:不错!
        和珏猫:@dapeng199 谢谢
      • 谭谭谭思密达:能在for in 里面进行数组元素的删除操作?还是说用了NSPredicate就可以
        和珏猫:@谭谭谭思密达 你是说随便一个数组进行删除吗?
        这里是对coredata里面的数据进行删除操作,NSPredicate是设置检索的条件,for in里面进行的是删除和保存的工作。不能理解为数组的增加和删除元素用这种方法。

      本文标题:coreData初步认识

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