0
我使用以下代碼從我的稱爲行業的CoreDataModel中取消屬性。不過我能夠使用fetchRequest.propertiesToFetch訪問還沒有要求這些屬性 讓fetchRequest = NSFetchRequest(的entityName: 「工業」) fetchRequest.propertiesToFetch = 「ID」, 「industry_name」]獲取CoreData中的特定屬性或屬性
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
}
請分享您的代碼 –
當您設置的屬性來獲取,但仍取型工業的對象,但會嘗試加快只讓每個請求的prorties抓取對象...這會導致元組= [行業]不是[(id,Industry_name)] – SeanLintern88
您可以通過將獲取請求的'resultType'指定爲'.DictionaryResultType'來修改此行爲,在這種情況下,獲取返回一個數組字典(不是元組),每個字典都有'propertiesToFetch'中指定的鍵和相應的屬性值。 – pbasdf