Programmatically Update an attribute in Core Data

前端 未结 5 722
被撕碎了的回忆
被撕碎了的回忆 2020-12-23 15:11

I\'ve looked through all the class documentation for Core Data and I can\'t find away to programmatically update values in a core data entity. For example, I have a structur

5条回答
  •  离开以前
    2020-12-23 15:31

    Sounds like you're thinking in terms of an underlying relational database. Core Data's API is built around model objects, not relational databases.

    An entity is a Cocoa object—an instance of NSManagedObject or some subclass of that. The entity's attributes are properties of the object. You use key-value coding or, if you implement a subclass, dot syntax or accessor methods to set those properties.

    Evan DiBiase's answer shows one correct way to set the property—specifically, an accessor message. Here's dot syntax:

    bookTwo.title = @"BarBar";
    

    And KVC (which you can use with plain old NSManagedObject):

    [bookTwo setValue:@"BarBar" forKey:@"title"];
    

提交回复
热议问题