结构,与核心数据对象类型同名?

toa*_*ast 5 swift

我可以有一个与 Core Data 对象类型同名的结构吗?如果是这样,我如何在代码中区分两者?

编辑:例如,我有一个Track核心数据对象,当我从外部读取“跟踪”信息时,它是通过 json 传入的。我没有使用核心数据对象,因为它是一个托管对象,所以我使用了另一种结构。我也打算命名它Track,但是这可能会导致我不确定的冲突,所以目前我TrackStruct改为调用它。另外,这是正确的方法吗?

谢谢!

nay*_*yem 2

在经历了很多困难之后,我为您制作了一个示例项目。但我在这里发布主要概念。

您可以在此处获取示例项目。虽然我已经从本地.plist文件加载了数据。您可以检查该loadPersonWithJSON(fromPath:)函数的工作。只需按照我的代码注释即可。

假设我有一个Person实体,Core-Data有两个String属性namelocation。从 json 中我得到了类型为 的数组[[String:Any]]。现在我想将 json 数据映射到核心数据对象模型。

enum CoreDataError: String, Error {
    case NoEntity = "ERROR: No Entity, Check the Entity Name"
}

enum JSONError: String, Error {
    case NoData = "ERROR: no data"
    case ConversionFailed = "ERROR: conversion from JSON failed"
}

typealias personJSONObjectType = [[String:String]]

class PersonTableViewController: UITableViewController {

    var person: [Person] = []

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.loadPersonWithJSON(fromPath: "your json URL in String format")
    }

    func loadPersonWithJSON(fromPath jsonURLString:String) {
        guard let jsonURL = URL(string: jsonURLString) else {
            print("Error creating an URL from \(jsonURLString)")
            return
        }
        URLSession.shared.dataTask(with: jsonURL) { (data, response, error) in
            do {
                guard let data = data else {
                    throw JSONError.NoData
                }
                guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? personJSONObjectType else {
                    throw JSONError.ConversionFailed
                }

                // Here you have your json data. Now map this data to your model object.

                // First you need to have your shared App Delegate
                guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
                    print("No shared AppDelegate")
                    return
                }

                // Use shared App Delegate to have the persistent containers view context as managed object context. This will be used to verify whether your Entity exists or not
                let managedObjectContext = appDelegate.persistentContainer.viewContext

                // Get the Entity in your core data model
                guard let entity = NSEntityDescription.entity(forEntityName: "Person", in: managedObjectContext) else {
                    throw CoreDataError.NoEntity
                }

                let persons = json.map({ (personInfo) -> Person in

                    let personName = personInfo["name"] as? String              // use appropriate key for "name"
                    let personLocation = personInfo["location"] as? String      // use appropriate key for "location"

                    // Get your object as Core data Managed object.
                    let aPerson = NSManagedObject(entity: entity, insertInto: managedObjectContext) as! Person

                    // Manipulate core data object with json data
                    aPerson.name = personName
                    aPerson.location = personLocation
                    // Manipulation done

                    return aPerson
                })

                self.person = persons
                self.tableView.reloadData()

            } catch let error as JSONError {
                print(error.rawValue)
            } catch let error as CoreDataError {
                print(error.rawValue)
            } catch let error as NSError {
                print(error.debugDescription)
            }
            }.resume()
    }
}
Run Code Online (Sandbox Code Playgroud)

额外资源

您可以使用以下表视图数据源方法来检查是否有效:

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.person.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    let aPerson = self.person[indexPath.row]
    cell.textLabel?.text = aPerson.name
    cell.detailTextLabel?.text = aPerson.location
    return cell
}
Run Code Online (Sandbox Code Playgroud)