I have a managed-object X with a relationship to a list of [1:N) Y objects. (X and Y objects are stored using Core Data)
I would like to find the proper (and more convenient) way to obtain the latest inserted Y object. The "name" attribute of this Y object should be shown in the graphic possibly through Cocoa Bindings.
I kno开发者_Go百科w that all Ys are inserted into a NSSet, so there is no defined order. However, each Y has a "timestamp" attribute, so when I say "the latest inserted Y object" I actually mean "the Y with the latest timestamp".
Any help will be appreciated: I searched all the documentation but have not found anything that could help me.
In The KVC Programming Guide's Collection Operators section, the description for @max says
The @max operator compares the values of the property specified by the key path to the right of the operator and returns the maximum value found. The maximum value is determined using the compare: method of the objects at the specified key path. The compared property objects must support comparison with each other. If the value of the right side of the key path is nil, it is ignored.
The following example returns the maximum value of the date values (date of the latest transaction) for the Transaction objects in transactions:
NSDate *latestDate = [transactions valueForKeyPath: "@max.date"];
The latestDate value (formatted) is Jul 15, 2010.
精彩评论