在CoreData中保存对象(带字符串,双精度和数组的属性)

时间:2018-04-05 12:09:59

标签: ios swift core-data

我在CoreData中保存对象时遇到了困难。它之前工作正常,但我将三个变量从一个字符串更改为一个字符串数组... image,imageDesc和attrib 我有一个名为CoreDataHandler的文件,如下所示。

class CoreDataHandler: NSObject {
private class func getContext() -> NSManagedObjectContext {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    return appDelegate.persistentContainer.viewContext
}
class func saveObject(name: String, type: String, description: String, image: [String], imageDesc: [String], attrib: [String], rating: String, address: String, lat: Double, long: Double, annoName: String) -> Bool {
    //cost: String, openingTimes: String, pets: String, 
    let context = getContext()
    let entity = NSEntityDescription.entity(forEntityName: "CastleSave", in: context)
    let manageObject = NSManagedObject(entity: entity!, insertInto: context)

    manageObject.setValue(name, forKey: "name")
    manageObject.setValue(type, forKey: "type")
    manageObject.setValue(description, forKey: "desc")
    manageObject.setValue([image], forKey: "image")
    manageObject.setValue([imageDesc], forKey: "imageDesc")
    manageObject.setValue([attrib], forKey: "attrib")
    manageObject.setValue(rating, forKey: "rating")
    manageObject.setValue(address, forKey: "address")
    manageObject.setValue(lat, forKey: "lat")
    manageObject.setValue(long, forKey: "long")
    manageObject.setValue(annoName, forKey: "annoName")

    do {
        try context.save()
        return true
    } catch {
        return false
    }
}

我使用以下代码保存为收藏夹

CoreDataHandler.saveObject(name: (txte?.name)!, type: (txte?.type)!, description: (txte?.description)!, image: (txte?.image)!, imageDesc: (txte?.imageDesc)!, attrib: (txte?.attrib)!, rating: (txte?.rating)!, address: (txte?.address)!, lat: (txte?.lat)!, long: (txte?.long)!, annoName: (txte?.annoName)!)

在我的DataModel中...... 我以前有图像属性,imageDesc&作为字符串。
我将其更改为Transformable,将属性CustomClass更改为[String]但我正在努力查看如何正确保存它。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

这并不是第一个例子,但是我不知道在哪里或如何创建这个代码。当使用核心数据时,它会越来越难以帮助解决这些复杂的问题。

创建模型时,您的应用应为您自动生成可用于直接与核心数据通信的文件。应该有与您的模型中命名相同的托管对象的子类。所以例如

let manageObject = NSManagedObject(entity: entity!, insertInto: context)

应改为

let castleSave = NSManagedObject(entity: entity!, insertInto: context) as! CastleSave

您应该可以像castleSave.name = name一样直接调用属性。

如果你有这个,你会发现你不能打电话给castleSave.image = [image],因为如果你做的正确,image属性就是Data。所以你需要序列化这些东西。

所以问题似乎是如何将图像数组转换为数据。我想JSONSerialization加上base64字符串编码应该这样做:

func dataFromImageArray(images: [UIImage]) -> Data? {
    let imageDataArray: [Data] = images.compactMap { UIImageJPEGRepresentation($0, 1.0) }
    let imageBase64Array: [String] = imageDataArray.compactMap { $0.base64EncodedString() }
    let jsonData: Data? = try? JSONSerialization.data(withJSONObject: imageBase64Array, options: [])
    return jsonData
}

func imageArrayFromData(data: Data) -> [UIImage] {
    guard let items = (try? JSONSerialization.jsonObject(with: data, options: [.allowFragments])) as? [String] else {
        return []
    }

    let dataItems: [Data] = items.compactMap { Data(base64Encoded: $0) }
    let images: [UIImage] = dataItems.compactMap { UIImage(data: $0) }

    return images
}

可以对任何可转换为数据的对象的数组进行非常相似的操作。