daveomcd
daveomcd

Reputation: 6565

Core Data: In XCode how can I set my attribute to be unique?

I'm setting up my Data Model for Core Data and I'm trying to find out how I can set up an attribute to be unique. I want it so that if another object is about the be saved then it wont allow it if this attribute is the same value as another. How can I do this? Thanks!

Upvotes: 3

Views: 3453

Answers (1)

Chris Trahey
Chris Trahey

Reputation: 18290

As specified in the Validation section of the CoreData guide, you can use the key-valu-coding validation patterns described in the KVC guide. Specifically, in the automatic validation section, it mentions that CoreData will use the KVC validation patterns when it tries to save. So on your model categories, you will end up with something like this:

-(BOOL) validateCourseName:(NSString **) courseName error:(NSError **) error {
  // lookup existing course names, return NO if one exists, YES if none.
  // note that courseName is a **, which means you can modify it if that makes sense.
  *courseName = @"new name which will validate";
  // but be sure to read the parts of the linked docs about memory if you do this.
  return YES
}

Upvotes: 2

Related Questions