1

Is there any way I can validate a value updated in a Core Data entity's property against values of the property in other entities in the collection?

At the moment I create an entity with some default values, add it to arrangedObjects, then get the user to modify the various property values. However, I would like to check a particular property and make sure there're no other entities in the array with the same value for that property. What's the best way to do this?

Many thanks, Dany.

codedog
  • 2,488
  • 9
  • 38
  • 67

1 Answers1

0

Manually checking is only a few lines of code with a fast enumeration loop:

BOOL unique = YES;
for (NSManagedObject *obj in collection) {
    if (obj.property == value) {
        unique = NO;
        break;
    }
}
gerry3
  • 21,420
  • 9
  • 66
  • 74
  • Thanks but where can I do this from? Sorry, I still have my head stuck in the .NET event model - is there something similar to "updating" event as such? – codedog Mar 10 '10 at 09:15
  • You probably want to do it before you dimiss the view controller where the user edits the values. – gerry3 Mar 10 '10 at 09:33
  • I am editing in the table view so the view controller hangs around throughout the app. – codedog Mar 10 '10 at 17:17
  • I see. Then you probably want to validate when the cell or field loses focus (maybe when the keyboard is dismissed). – gerry3 Mar 10 '10 at 18:17
  • Thanks - that makes sense. One more question if I may please. I have had a look through the reference for NSTextFieldCell and cannot find a way of handling the field/cell losing focus. In fact, the whole event thing in Cocoa is still very fuzzy for me. Can you please point me in the right general direction for event handling? – codedog Mar 10 '10 at 19:47
  • Hang on...just noticed something...you keep referring to keyboard being dismissed. Are you referring to iPhone apps? I am writing a Mac app at the moment... – codedog Mar 10 '10 at 21:20
  • Oops, I am used to thinking in terms of iPhone. I still think you should look at when the cell or field loses focus, but I haven't worked with an NSTableView. – gerry3 Mar 10 '10 at 23:26