Custom setter methods in Core-Data

前端 未结 5 1121
夕颜
夕颜 2020-12-07 18:09

I need to write a custom setter method for a field (we\'ll call it foo) in my subclass of NSManagedObject. foo is defined in the data

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 18:55

    Here is the Apple way for overriding NSManagedObject properties (without breaking KVO), in your .m file:

    @interface Transaction (DynamicAccessors)
    - (void)managedObjectOriginal_setDate:(NSDate *)date;
    @end
    
    @implementation Transaction
    @dynamic date;
    
    - (void)setDate:(NSDate *)date
    {
        // invoke the dynamic implementation of setDate (calls the willChange/didChange for you)
        [self managedObjectOriginal_setDate:(NSString *)date;
    
        // your custom code
    }
    

    managedObjectOriginal_propertyName is a built-in magic method you just have to add the definition for. As seen at bottom of this page What's New in Core Data in macOS 10.12, iOS 10.0, tvOS 10.0, and watchOS 3.0

提交回复
热议问题