【发布时间】:2017-09-02 10:18:22
【问题描述】:
我正在使用以下代码来消除属性,同时从名为行业的 CoreDataModel 中获取。我仍然可以使用 fetchRequest.propertiesToFetch 访问那些没有请求的属性 让 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
}
【问题讨论】:
-
请分享您的代码
-
当您将属性设置为 fetch 时,它仍然会获取 Industry 类型的对象,但会尝试通过仅获取每个对象上请求的属性来加快获取速度...这会导致 tuples = [行业] 不是 [(id, Industry_name)]
-
您可以通过将获取请求的
resultType指定为.DictionaryResultType来修改此行为,在这种情况下,获取返回一个字典数组(不是元组),每个字典都有指定的键在propertiesToFetch和相应的属性值中。
标签: ios core-data ui-automation swift3.1