来自自定义子类的变量不会 saved/won 不会从 Core Data 加载
Variables from custom subclass aren't saved/won't load from Core Data
我正在制作一个允许用户保存和重新打开绘图的绘图应用程序,因此我需要使用 CoreData。其中一项功能是能够更改路径颜色。我需要在每个 UIBezierPath 中存储一种颜色,以便在绘制时以正确的颜色描边。因此我 subclassed UIBezierPath:
class colorPath: UIBezierPath {
var pathStrokeColor: UIColor?
}
然后我在 draw(_ rect:) 函数中绘制这样的路径:
for path in paths{
path.pathStrokeColor?.setStroke()
path.stroke()
}
在我关闭应用程序并重新打开之前,它工作正常。 pathStrokeColor 只有一个 nil 值,并以默认颜色 -- 黑色绘制。
路径数组的数组是这样保存在Core Data实体JFile中的。 "thearray" 是可转换的类型,所以我将 colorPath 转换为 NSObject:
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let afile = JFile(context: context)
afile.thearray = arrayOfPages as NSObject
(UIApplication.shared.delegate as! AppDelegate).saveContext()
路径数组的数组是这样检索的。我必须将 NSObject 数组转换回 colorPath:
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let jfiles = try context.fetch(JFile.fetchRequest())
let filez = files as! [JFile]
if filez.count>0{
arrayOfPages = filez[myindex].thearray as! [[colorPath]]
}
所以我假设我错误地子classing UIBezierPath,因为与 Core Data 一起工作的唯一变量是我自己创建的变量 -- pathStrokeColor。我的语法错了吗?这与 pathStrokeColor 是可选的有关吗?是不是因为我不能class my subclass 到NSObject?我该如何解决这个问题?
您不能将任何对象保存到核心数据中。它必须是数字、日期、字符串或数据。通常,如果您想存储除此之外的其他内容,您首先将其转换为数据以存储它并在您想要读取它时从数据中解码它。根据您共享的代码,似乎没有任何地方发生这种情况。
当 属性 可变形时,它允许您将任何符合 NSCoding
的对象存储到核心数据中。但这不是魔术。符合 NSCoding
的对象实现 init?(coder aDecoder: NSCoder)
和 encode(with aCoder: NSCoder)
,它们将对象与数据相互转换。
A UIBezierPath
已经符合 NSCoding
,因此 技术上 您的子类也符合。但是您的子类没有存储其额外的 属性。您还必须在子类中实现编码:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
if let color = aDecoder.decodeObject(forKey: "pathStrokeColor") as? UIColor{
pathStrokeColor = color
}
}
override func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
if let color = pathStrokeColor{
aCoder.encode(color, forKey: "pathStrokeColor")
}
}
我正在制作一个允许用户保存和重新打开绘图的绘图应用程序,因此我需要使用 CoreData。其中一项功能是能够更改路径颜色。我需要在每个 UIBezierPath 中存储一种颜色,以便在绘制时以正确的颜色描边。因此我 subclassed UIBezierPath:
class colorPath: UIBezierPath {
var pathStrokeColor: UIColor?
}
然后我在 draw(_ rect:) 函数中绘制这样的路径:
for path in paths{
path.pathStrokeColor?.setStroke()
path.stroke()
}
在我关闭应用程序并重新打开之前,它工作正常。 pathStrokeColor 只有一个 nil 值,并以默认颜色 -- 黑色绘制。
路径数组的数组是这样保存在Core Data实体JFile中的。 "thearray" 是可转换的类型,所以我将 colorPath 转换为 NSObject:
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let afile = JFile(context: context)
afile.thearray = arrayOfPages as NSObject
(UIApplication.shared.delegate as! AppDelegate).saveContext()
路径数组的数组是这样检索的。我必须将 NSObject 数组转换回 colorPath:
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let jfiles = try context.fetch(JFile.fetchRequest())
let filez = files as! [JFile]
if filez.count>0{
arrayOfPages = filez[myindex].thearray as! [[colorPath]]
}
所以我假设我错误地子classing UIBezierPath,因为与 Core Data 一起工作的唯一变量是我自己创建的变量 -- pathStrokeColor。我的语法错了吗?这与 pathStrokeColor 是可选的有关吗?是不是因为我不能class my subclass 到NSObject?我该如何解决这个问题?
您不能将任何对象保存到核心数据中。它必须是数字、日期、字符串或数据。通常,如果您想存储除此之外的其他内容,您首先将其转换为数据以存储它并在您想要读取它时从数据中解码它。根据您共享的代码,似乎没有任何地方发生这种情况。
当 属性 可变形时,它允许您将任何符合 NSCoding
的对象存储到核心数据中。但这不是魔术。符合 NSCoding
的对象实现 init?(coder aDecoder: NSCoder)
和 encode(with aCoder: NSCoder)
,它们将对象与数据相互转换。
A UIBezierPath
已经符合 NSCoding
,因此 技术上 您的子类也符合。但是您的子类没有存储其额外的 属性。您还必须在子类中实现编码:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
if let color = aDecoder.decodeObject(forKey: "pathStrokeColor") as? UIColor{
pathStrokeColor = color
}
}
override func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
if let color = pathStrokeColor{
aCoder.encode(color, forKey: "pathStrokeColor")
}
}