【问题标题】:How to save in core data?如何保存核心数据?
【发布时间】:2017-10-14 00:11:06
【问题描述】:

我一直在寻找一些关于如何在 Core Data 中存储 NSArray 的示例代码,但没有任何运气。我现在知道如何存储 NSStrings、NSDate 和其他类型的数据,但很难存储 NSArray。我读过很多文章说你必须将它写入磁盘并写入文件,但我似乎无法理解。

import UIKit

class Person: NSObject {
var fullName : String!
var mobileNumbers : [Mobile]!
}

import UIKit

class Mobile: NSObject {

    var mobileNumber : String!
 }

// core data save method

以下方法显示保存到核心数据

 func save(name: Person) {


     guard let appDelegate =
            UIApplication.shared.delegate as? AppDelegate else {
                return
        }
         var managedContext = NSManagedObjectContext()

        if #available(iOS 10.0, *) {
             managedContext = appDelegate.persistentContainer.viewContext
        } else {
            // Fallback on earlier versions
        }


        let entity = NSEntityDescription.entity(forEntityName: "Contact",in: managedContext)!

        let person = NSManagedObject(entity: entity,insertInto: managedContext)

         person.setValue(name.fullName, forKeyPath: "fullName")

       if let mob = name.mobileNumberList{

            let arch = NSKeyedArchiver.archivedData(withRootObject: mob[0])
            print(arch)

            person.setValue(arch, forKey: "mobileNumberList")

        }



        do {
            try managedContext.save()
        } catch let error as NSError {
            print("Could not save. \(error), \(error.userInfo)")
        }


    }

【问题讨论】:

  • coredata 没有使用 Archiver,而是提供了一种称为 Transformable 的选项数据类型。您可以在您的实体中将您的数据类型设置为可转换的。在此链接上查看我的答案stackoverflow.com/questions/8682324/…replace dictionary with array type
  • 对于mobileNumberList 属性,您似乎已经有了执行此操作的代码。它不工作吗?如果没有,具体出了什么问题?

标签: ios swift core-data binary-data


【解决方案1】:

你可以在核心数据中存储一个数组

在 .xcdatamodeld 文件中创建实体名称“Contact”并添加一个属性(任何名称)并选择类型“Transformable”。

然后

 let entity = NSEntityDescription.entity(forEntityName: "Contact",in: managedContext)!

 let person = NSManagedObject(entity: entity,insertInto: managedContext)

 person.setValue("YourArrayName", forKeyPath: "YourArrayKeyName")

查看link 了解更多关于 swift 中核心数据工作的详细信息

【讨论】:

    【解决方案2】:

    试试这个:-

       guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
    
                    let managedContext = appDelegate.persistentContainer.viewContext
                    let user:UserModel =  NSEntityDescription.insertNewObject(forEntityName: "User", into: managedContext) as! UserModel
    
    
                    user.name = txtName.text!
    
                  var arr: [UInt32] = [32, 4, UInt32.max]
             let data = Data(buffer: UnsafeBufferPointer(start: &arr, count: arr.count))                
    
                    user.arr = data
    
    
    
                    do {
                        try managedContext.save()
    
    
                    } catch let error as NSError
                    {
                        print("Could not save. \(error), \(error.userInfo)")
                    }
    

    【讨论】:

    • 根据你的改变数组类型@venkat
    【解决方案3】:

    简短的回答是你不能。

    你有两个选择:

    1. 为您的对象 Mobile(在您的数据模型中)创建一个新的核心数据实体,并使用 1:many 关系链接到 Person。 Core Data 会自动为您生成方法(例如 addToMobiles(_ value: Mobile))。

    2. 将 Person 的属性建模为“mobileNumbers”作为 NSString。然后将手机号码存储为 JSON blob(或任何其他结构)

    第一个可能更“正确”,但如果您只存储数字,则第二个更容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2020-05-02
      相关资源
      最近更新 更多