slimboy slimboy - 5 months ago 24
Swift Question

Grab the highest date inside of an array of entities.

I have the following array :

let messages = (fetchedResultsController.fetchedObjects as! [Message])

essentially, each element in this array is of the entity Message, where each Message has 5 core data attributes. Notably, the attribute I care about in this case is timestamp which is of type
!!!!!!. I want to grab the message with the maximum date inside of this array using the reduce function. Please do not suggest to sort the messages first. I'am trying to avoid that. Thank you !


Not sure why you would want to use reduce, but I think this will work and fits with what you are looking for (assuming Swift 3):

let result = messages.max(by: {$0.timeStamp < $1.timeStamp})