[英]How to save String Array to Core Data in Swift
我需要将我的字符串数组保存到核心数据。
let stringArray: [String] = ["First String", "Second String", "Third String"]
我有1个类型为String的属性。 我已经尝试过了,但是没有用。
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个属性添加为字符串的“值”类型。
现在选择该实体,然后从编辑器菜单->创建NSManagedObject类
将数据保存在实体中
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.