iOS - Core Data集属性主键 [英] iOS - Core Data set attribute primary key

查看:226
本文介绍了iOS - Core Data集属性主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift开发一个应用程序,最近开始使用Core Data。
我必须定义我的实体的哪个属性将是我的主键。例如:



我有一个具有类属性的实体:




  • id



  • 我需要id属性是我的主键。



    在Objective-C中可能是一样的,只需要知道如何定义它。

    解决方案

    每个 NSManagedObject 都有自己的唯一键, $ c> objectID 属性。



    此ID在内部用于链接实体的关系。



    你总是可以通过获取id。 NSManagedObject.objectID



    使用 NSMananagedObjectContext.objectWithId(...)


    I am developing an application in Swift and started using Core Data recently. I must define which attribute to my entity will be my primary key. For example:

    I have an entity that has the attributes of the class:

    • id
    • name
    • age

    I need the "id" attribute is my primary key.

    May be the same in Objective-C, just need to know how to define it.

    解决方案

    Each NSManagedObject has its own unique key built in which is available in its objectID property.

    This id is internally used to link entities over its relations. There is no need to maintain an own id as a primary key as you are used to do in SQL.

    You can always get the id by NSManagedObject.objectID.

    Fetching an object by its id can be performed directly by the managed object context using NSMananagedObjectContext.objectWithId(...)

    这篇关于iOS - Core Data集属性主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆