Custom setter methods in Core-Data

前端 未结 5 1118
夕颜
夕颜 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:48

    According to the documentation, it'd be:

    - (void) setFoo:(NSObject *)inFoo {
      [self willChangeValueForKey:@"foo"];
      [self setPrimitiveValue:inFoo forKey:@"foo"];
      [self didChangeValueForKey:@"foo"];
    }
    

    This is, of course, ignoring the fact that NSManagedObjects only want NSNumbers, NSDates, NSDatas, and NSStrings as attributes.

    However, this might not be the best approach. Since you want something to happen when the value of your foo property changes, why not just observe it with Key Value Observing? In this case, it sounds like "KVO's the way to go".

提交回复
热议问题