Ios 如何在Swift中将字符串数组保存到核心数据
我需要将字符串数组保存到核心数据Ios 如何在Swift中将字符串数组保存到核心数据,ios,arrays,swift3,save,swift4,Ios,Arrays,Swift3,Save,Swift4,我需要将字符串数组保存到核心数据 let stringArray: [String] = ["First String", "Second String", "Third String"] 我有1个字符串类型的属性。我试过了,但没用 let entityCoreData = NSEntityDescription.insertNewObject(forEntityName: "Hadith", into: context) entityCoreData.setValue(firstEntit
let stringArray: [String] = ["First String", "Second String", "Third String"]
我有1个字符串类型的属性。我试过了,但没用
let entityCoreData = NSEntityDescription.insertNewObject(forEntityName: "Hadith", into: context)
entityCoreData.setValue(firstEntity, forKey: "firstAttribute")
do {
try context.save()
} catch {
print("issue by saving data")
}
在.xcDataModel中添加实体作为“Hadith”&添加1个属性作为字符串的“value”类型。
现在从编辑器菜单->创建NSManagedObject类中选择该实体(&O) 在实体中保存数据
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context = appDelegate.managedObjectContext
for str in strinArray {
do {
let newitem = NSEntityDescription.insertNewObjectForEntityForName("Hadith", inManagedObjectContext: context)
newitem.setValue(str,forKey: "value")
try context.save()
} catch {
//do nothing
}
这将在尝试仅保存数组的一个元素时插入整个新对象