0

I have an NSArray of data objects Books retrieved from Core Data.

For one of the attributes or variables in core data, rating, I would like to find its max.

I am able to get an array of ratings as follow:

NSMutableArray *ratingsArray = [theBooks valueForKeyPath:@"rating"];
    NSLog(@"ratingsArray%@",ratingsArray);//this logs fine

However, I am stuck on how to get the max.

_maxRating = [ratingsArray valueForKeyPath:@"@max.rating.self"];

builds but gives an error at runtime:

this class is not key value coding-compliant for the key rating.'

(Adding to my confusion, I have read that an array should not be able to hold a float, however, mine does to the degree that I can log out the ratings in the above code fine. Rating is a float value in the NSManaged Object Class and also in the core data model. It is originally retrieved from an API as a string, and converted to a float prior to storage in core data using [ratingString floatValue])

What is the correct way to get the max for an attribute in an array of objects?

zztop
  • 701
  • 1
  • 7
  • 20
  • Regarding your error "this class..." You are probably accessing a non-existent property. "max.rating.self" , doesnt make any sense what you are trying to access. Also, it is impossible to help you since we have no idea what "Books" are , what properties you have, what you are trying to access and achieve. –  Feb 19 '17 at 14:51
  • Figured it out. The ratingArray was already an array exclusively of ratings so no need to reference rating in the code for max. _maxRating = [ratingsArray valueForKeyPath:@"@max.self"]; works – zztop Feb 19 '17 at 15:09
  • Great! Glad it solved. –  Feb 19 '17 at 15:12

0 Answers0