How to group by the elements of an array in Swift

Swift 4: Since Swift 4, this functionality has been added to the standard library. You can use it like so: Dictionary(grouping: statEvents, by: { $0.name }) [ “dinner”: [ StatEvents(name: “dinner”, date: “01-01-2015”, hours: 1), StatEvents(name: “dinner”, date: “01-01-2015”, hours: 1), StatEvents(name: “dinner”, date: “01-01-2015”, hours: 1) ], “lunch”: [ StatEvents(name: “lunch”, date: “01-01-2015”, hours: … Read more

How can I reverse a NSArray in Objective-C?

There is a much easier solution, if you take advantage of the built-in reverseObjectEnumerator method on NSArray, and the allObjects method of NSEnumerator: NSArray* reversedArray = [[startArray reverseObjectEnumerator] allObjects]; allObjects is documented as returning an array with the objects that have not yet been traversed with nextObject, in order: This array contains all the remaining … Read more