ios - Variables from custom subclass aren't saved/won't load from Core Data -
i making drawing application allows users save , reopen drawings, hence why need use coredata. 1 of features ability change path colour. need store colour in each uibezierpath strokes right colour when gets drawn. hence subclassed uibezierpath:
class colorpath: uibezierpath { var pathstrokecolor: uicolor? }
i draw paths in draw(_ rect:) function:
path in paths{ path.pathstrokecolor?.setstroke() path.stroke() }
this works fine until close application , reopen it. pathstrokecolor has nil value, , draws in default colour -- black.
the array of path arrays saved in core data entity jfile this. "thearray" of type transformable, convert colorpath nsobject:
let context = (uiapplication.shared.delegate as! appdelegate).persistentcontainer.viewcontext let afile = jfile(context: context) afile.thearray = arrayofpages nsobject (uiapplication.shared.delegate as! appdelegate).savecontext()
the array of path arrays retrieved this. have convert nsobject array 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]] }
so assume subclassing uibezierpath incorrectly because variable work core data 1 made myself -- pathstrokecolor. syntax wrong? pathstrokecolor being optional? because can't class subclass nsobject? how fix this?
you can't save object core-data. must number, date, string or data. if want store other convert data first store , decode data when want read it. based on code shared there not appear anywhere taking place.
when property transformable allows store object conforms nscoding
core-data. not magic. objects conform nscoding
implement init?(coder adecoder: nscoder)
, encode(with acoder: nscoder)
convert object , data.
a uibezierpath
conforms nscoding
, technically subclass well. subclass not storing property. have implement encoding in subclass:
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") } }
Comments
Post a Comment