我正在使用以下代码来消除属性,同时从名为行业的 CoreDataModel 中获取。我仍然可以使用fetchRequest.propertiesToFetch访问那些没有请求的属性 let fetchRequest = NSFetchRequest(entityName: "Industry") fetchRequest.propertiesToFetch = ["id","industry_name"]
After this i am using following code:
industryObject.industry_name=tuple.value(forKey: ""ind_subtype"") as? String
"ind_subtype" i have not specified in *.propertiesToFetch* and i am still able to access it
func fetchIndustryNamesWithId()->[IndustryData]{
var industryData=[IndustryData]()
//fetchRequest.predicate = NSPredicate(format: "firstName == %@", firstName)
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Industry")
fetchRequest.propertiesToFetch = ["id","industry_name"]
do {
let tuples = try managedObjectContext.fetch(fetchRequest)
for tuple in tuples{
let industryObject=IndustryData()
industryObject.id=tuple.value(forKey: "id") as? Int
industryObject.industry_name=tuple.value(forKey: "industry_name") as? String
industryObject.ind_subtype=tuple.value(forKey: "ind_subtype") as? String
industryData.append(industryObject)
}
return industryData
} catch {
let fetchError = error as NSError
print(fetchError)
}
return industryData
}