Swift formatters

The easiest way to get a formatted element is to use the formatted method that is available on a myriad of data types without the need to instantiate our own formatter. And the best thing is that all these things are also available directly on SwiftUI controls like Text and TextField by using the format parameter. Below are the most common/useful ones I’ve found so far. Dates In order to get a date formatted, we can simply call formatted on a Date type, which gives us the full, long formatted date:

Grouped queries in SwiftUI

Querying While developing Maby I needed to retrieve all events that happened, grouped by dates. Now of course I could just slap a FetchRequest and then manually group the results into a dictionary, specially given how easy it is with the grouping init, but it turns out that there’s a better way. Since iOS/iPadOS/tvOS 15, macOS 12 and watchOS 8 there’s a new fetch request in town called SectionedFetchRequest that allows us to specify a type to group by and an entity to group, so for example in my case I could just do the following:

Unrecognized selector objectAtIndex

Unrecognized selector objectAtIndex 2022-06-21 22:41:30.447763+0200 App[56115:1179524] -[__NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x600003df9c20 This happened with the following code, basically when trying to do a ForEach on a property coming from a CoreData model: import CoreData import SwiftUI struct BudgetView: View { @FetchRequest(entity: CategoryGroup.entity(), sortDescriptors: []) var categoryGroups: FetchedResults<CategoryGroup> var body: some View { VStack { List { ForEach(categoryGroups, id: .id) { group in Text(group.name) ForEach(group.categories, id: .id) { category in Text(category.