I am trying to fetch data grouped by a given column. It works well when I have data. I want to handle the case when I have no data, because it raises an NS error that I could not catch in swift do catch block. I've seen the answers on creating an ObjC wrapper but I it does not apply to my case because I need to return an Array of String.
let request = self.fetchRequest()
request.propertiesToGroupBy = [attribute]
request.propertiesToFetch = [attribute]
request.resultType = NSFetchRequestResultType.dictionaryResultType
request.returnsDistinctResults = true
if let results = try? context().fetch(request), // raises exception in testing, but runs fine when run on simulator.
let dics = results as? [NSDictionary] {
var resultsArray: [Any] = []
for dic in dics {
if let propValue = dic[attribute] {
resultsArray.append(propValue)
}
}
return resultsArray
}
How might I do this?