获取汇总数据时,我试图获取由给定列分组数据抛出。当我有数据时它运作良好。我想在没有数据的情况下处理这个案例,因为它引发了一个NS错误,我无法在快速捕获块中捕获这个错误。我已经看到了创建一个ObjC包装的答案,但我不适用于我的情况,因为我需要返回一个String数组。如何处理NSInvalidArgumentException从空的持久存储
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
}
我该怎么做?
直到我找到一个真正的解决方案,继续前进,我会两次取。首先检查是否有任何数据,然后运行实际的提取。如果没有数据,则初始化为空数组。 –