具有可转换属性的 CoreData 保存失败
CoreData with transformable attribute fails on save
我想使用 CoreData 并在其中一个实体中保存自定义 class 属性。这样做的方法似乎是使用 Transformable:
然后我尝试添加一个元素并将其保存为:
import UIKit;
import CoreData;
public class EntityData: NSObject, NSSecureCoding
{
public static var supportsSecureCoding: Bool { true }
public func encode(with coder: NSCoder) {
coder.encode(self.name, forKey: "name");
}
public required init?(coder: NSCoder) {
self.name = coder.decodeObject(forKey: "name") as? String;
}
var name: String?;
init(name: String?) {
self.name = name;
}
}
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(_: animated);
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext;
let entity = Entity(context: context);
entity.id = URL(string: "google.com");
entity.data = EntityData(name: "Foo");
do {
try context.save();
} catch {
print("While saveing: \(error)");
}
}
}
这失败了:
2021-09-30 13:12:58.865312+0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f6040> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f6040> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-09-30 13:12:58.865530+0400 tmpCoreData[5815:170015] [error] error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-09-30 13:12:58.865983+0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034e4300> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034e4300> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-09-30 13:12:58.866158+0400 tmpCoreData[5815:170015] [error] error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-09-30 13:12:58.866558+0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034ec0c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034ec0c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-09-30 13:12:58.900366+0400 tmpCoreData[5815:170015] [error] error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-09-30 13:12:58.912131+0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f83c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f83c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
While saveing: Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred."
我不确定我做错了什么。这是我所有的代码,我没有添加任何其他代码。
可转换属性需要 class 符合 NS(Secure)Coding
(不是 Codable
!)和归档对象的实现。请注意 NSSecureUnarchiveFromData
错误。
在 Swift 世界中,考虑将属性声明为 String
,使用 Codable
将对象编码为 JSON 并添加计算的 属性无缝转换。
我想使用 CoreData 并在其中一个实体中保存自定义 class 属性。这样做的方法似乎是使用 Transformable:
然后我尝试添加一个元素并将其保存为:
import UIKit;
import CoreData;
public class EntityData: NSObject, NSSecureCoding
{
public static var supportsSecureCoding: Bool { true }
public func encode(with coder: NSCoder) {
coder.encode(self.name, forKey: "name");
}
public required init?(coder: NSCoder) {
self.name = coder.decodeObject(forKey: "name") as? String;
}
var name: String?;
init(name: String?) {
self.name = name;
}
}
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(_: animated);
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext;
let entity = Entity(context: context);
entity.id = URL(string: "google.com");
entity.data = EntityData(name: "Foo");
do {
try context.save();
} catch {
print("While saveing: \(error)");
}
}
}
这失败了:
2021-09-30 13:12:58.865312+0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f6040> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f6040> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-09-30 13:12:58.865530+0400 tmpCoreData[5815:170015] [error] error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-09-30 13:12:58.865983+0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034e4300> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034e4300> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-09-30 13:12:58.866158+0400 tmpCoreData[5815:170015] [error] error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-09-30 13:12:58.866558+0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034ec0c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034ec0c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
2021-09-30 13:12:58.900366+0400 tmpCoreData[5815:170015] [error] error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
CoreData: error: -executeRequest: encountered exception = <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo = (null)
2021-09-30 13:12:58.912131+0400 tmpCoreData[5815:170015] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f83c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x6000034f83c0> , <shared NSSecureUnarchiveFromData transformer> threw while encoding a value. with userInfo of (null)
While saveing: Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred."
我不确定我做错了什么。这是我所有的代码,我没有添加任何其他代码。
可转换属性需要 class 符合 NS(Secure)Coding
(不是 Codable
!)和归档对象的实现。请注意 NSSecureUnarchiveFromData
错误。
在 Swift 世界中,考虑将属性声明为 String
,使用 Codable
将对象编码为 JSON 并添加计算的 属性无缝转换。