Why can't I use “description” as an attribute name for a Core Data entity?

前端 未结 3 1778
深忆病人
深忆病人 2020-12-15 19:02

I have a simple Core Data entity that had a string attribute named \"description\". The program crashes when it hits:

valueForKey:@\"description\"

相关标签:
3条回答
  • 2020-12-15 19:12

    description isn't a reserved keyword in CoreData, but it's a property on all Objective-C objects inherently. It's part of the NSObject class.

    http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/description

    0 讨论(0)
  • 2020-12-15 19:28

    Because it conflicts with the -description method in NSObject (recall that Core Data dynamically generates property accessors and mutators — a property named ‘description’ would require creating an accessor method called -description). This is documented in the Core Data Programming Guide and the NSPropertyDescription Class Reference:

    Note that a property name cannot be the same as any no-parameter method name of NSObject or NSManagedObject. For example, you cannot give a property the name "description". There are hundreds of methods on NSObject which may conflict with property names—and this list can grow without warning from frameworks or other libraries. You should avoid very general words (like "font”, and “color”) and words or phrases which overlap with Cocoa paradigms (such as “isEditing” and “objectSpecifier”).

    0 讨论(0)
  • 2020-12-15 19:28

    I suspect (though I'm not positive) that the issue is Core Data's runtime accessor generation that is at issue. Core Data synthesizes accessors (and setters) for attributes at runtime and adds those accessors to the appropriate class (again, at runtime). If Core Data creates a new description method, overriding -[NSObject description] and putting transaction logic etc. into the method, then any code which calls -[NSObject description] might behave "badly".

    0 讨论(0)
提交回复
热议问题