ios - To fetch unique Attribute/Column values in Core Data -
i have entity calculatorproduct attributes 'id','brand','category', 'mrp' want fetch values of 'category' column/attribute
let context = databasecontroller.getcontext() let fetchrequest:nsfetchrequest<calculatorproduct> = calculatorproduct.fetchrequest() fetchrequest.propertiestofetch = ["category"] fetchrequest.returnsdistinctresults = true do{ let result = try context.fetch(fetchrequest) print("array db \(string(describing: result.count))") print("array db \(string(describing: result))") val in result{ print("\(string(describing: val.category))") } } catch{ print("error in fetching is\(error)") }
try following:
let context = databasecontroller.getcontext() let fetchrequest:nsfetchrequest<calculatorproduct> = calculatorproduct.fetchrequest() fetchrequest.propertiestofetch = ["category"] fetchrequest.returnsdistinctresults = true fetchrequest.resulttype = .dictionaryresulttype { let result = try context.fetch(fetchrequest) as! [[string:any]] print("array db \(string(describing: result.count))") print("array db \(string(describing: result))") val in result{ print("\(string(describing: val["category"]))") } } catch{ print("error in fetching is\(error)") }
Comments
Post a Comment