Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

objective c - Core data - implementing a custom to one relationship

I am not sure if this is a dumb question:

I have an managed object A and managed object A has a many to one relationship with table B.

I set the relationship A to B by doing something like

 Aobject.Bobject=acopyOfB;

Which is fine.

I want to write a custom setter for that relationship. This is so I can setup some read only properties in A, based on the value that B gets set to.

So I want to fill in the gap:

    -(void) setBobject:(Bobject)theValue{


  'the gap'

  }

I am happy with being able to set my custom values, but can't find anywhere the correct syntax for setting the relationship within core data.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You will need to do something like

- (void) setBObject:(BObject*)theValue {
  [self willChangeValueForKey:@"bObject"];
  [self setPrimitiveBObject:theValue];
  [self didChangeValueForKey:@"bObject"];

  // Do whatever else you need to do here
}

To avoid compiler warnings, you can declare the following before the @implementation directive (but in the .m file):

@interface MyManagedObjectSubClass (PrimitiveAccessors)

@property (nonatomic, retain) BObject* primitiveBObject;

@end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...