简体   繁体   English

如何在Swift中将字符串数组保存到核心数据

[英]How to save String Array to Core Data in Swift

I need to save my String Array to Core Data. 我需要将我的字符串数组保存到核心数据。

let stringArray: [String] = ["First String", "Second String", "Third String"]

I have 1 Attributes with the type String. 我有1个类型为String的属性。 I have tried that, but it doesn't work. 我已经尝试过了,但是没有用。

let entityCoreData = NSEntityDescription.insertNewObject(forEntityName: "Hadith", into: context)

entityCoreData.setValue(firstEntity, forKey: "firstAttribute")

do {
    try context.save()
} catch {
    print("issue by saving data")
}

Add Entity in .xcDataModel as "Hadith" & add 1 attribute as "value" type of string. 将.xcDataModel中的实体添加为“ Hadith”,并将1个属性添加为字符串的“值”类型。
Now select that entity & from Editor Menu -> Create NSManagedObject class 现在选择该实体,然后从编辑器菜单->创建NSManagedObject类

Save data in entity 将数据保存在实体中

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM