0

I am rather confused about key-value-coding and core data.

The Apple docs show making this call

NSDate *latestDate = [transactions valueForKeyPath:@"@max.date"];

but when I add it to my own class, I get this exception

Exception [<MyClass 0x171be8>  valueForUndefinedKey:]: this class is not key value coding-compliant for the key @max.

What am I missing?

darren
  • 579
  • 8
  • 23
  • What is the name of your `NSDate` property? – Jacob Relkin Jan 15 '13 at 07:23
  • That specific expression only works on a collection object, since it's querying the objects in itself to find the maximum value for the specified key path. You should be using `NSFetchRequest` with an `NSPredicate` to fetch objects from your backing persistent store using this criteria. – Jacob Relkin Jan 15 '13 at 07:30
  • Working off of what I said above, the reason why you're getting that error is because instances of `NSManagedObject` (or any non-collection object) don't know how to deal with a predicate expression such as `"@max"` when given to `-valueForKeyPath:` - only collections do. – Jacob Relkin Jan 15 '13 at 07:35
  • Ah, okay. I have been running that code against an instance, not a collection. I read it was magical, but it's not that magical :) So I still need to write an NSFetchRequest and NSPredicate type code block after all. – darren Jan 15 '13 at 08:04

1 Answers1

0

This error indicates that your Transaction entity (or whatever the entity contained in transactions is) has a date attribute with a different name or type.

Please check the data model and your NSManagedObject subclass to make sure it is indeed date and of type "Date"(data model) and "NSDate" (subclass).

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • The "transaction" object came from Apple documentation. My date field is called "modified". Its looking like the @max operator is only known to a collection and not an instance. – darren Jan 15 '13 at 08:06
  • Of course. It says transaction**s**, indicating an array or set. So, having a collection of your entity objects, `valueForKeyPath:@"@max.modified"` should work. – Mundi Jan 15 '13 at 11:07