I already read several posts and tried every solution, nothing works in my case.
I am using this complex data structure and need to store array of DrawnObjects in file. But it crashing when it first came to encode a variable which itself is an array of Structure type. Any help ?
[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x1702668c0
enum ArchiverKeys : String
{
case imageView = "ImageView"
case stateArray = "StateArray"
case bezierPathArray = "BezierPathArray"
case reactangleArray = "ReactangleArray"
case deleted = "Deleted"
}
struct RectanglePath {
var point1: CGPoint
var point2: CGPoint
var point3: CGPoint
var point4: CGPoint
}
struct StateObject {
var isAdd = true
var path = String()
}
class DrawObject: NSObject , NSCoding {
var ImageView = UIImageView()
var arrStates = [StateObject]()
var arrBezierPaths = [UIBezierPath]()
var rects = [RectanglePath]()
var deleted = false
override init() {
ImageView = UIImageView()
arrStates = []
arrBezierPaths = []
rects = []
deleted = false
}
func encode(with archiver: NSCoder) {
archiver.encode(self.ImageView, forKey:ArchiverKeys.imageView.rawValue )
archiver.encode(self.arrStates, forKey: ArchiverKeys.stateArray.rawValue)
archiver.encode(self.arrBezierPaths, forKey:ArchiverKeys.bezierPathArray.rawValue )
archiver.encode(self.rects, forKey: ArchiverKeys.reactangleArray.rawValue)
archiver.encode(self.deleted, forKey: ArchiverKeys.deleted.rawValue)
}
required convenience init(coder unarchiver: NSCoder) {
self.init()
self.ImageView = unarchiver.decodeObject(forKey: ArchiverKeys.imageView.rawValue) as! UIImageView
self.arrStates = unarchiver.decodeObject(forKey: ArchiverKeys.stateArray.rawValue) as! [StateObject]
self.arrBezierPaths = unarchiver.decodeObject(forKey: ArchiverKeys.bezierPathArray.rawValue) as! [UIBezierPath]
self.rects = unarchiver.decodeObject(forKey: ArchiverKeys.reactangleArray.rawValue) as! [RectanglePath]
self.deleted = (unarchiver.decodeObject(forKey: ArchiverKeys.deleted.rawValue) != nil)
}
}
func saveArrayTo(_ directoryName: String , arrayToSave: NSArray) {
// let encodedData = NSKeyedArchiver.archivedData(withRootObject: arrayToSave)
NSKeyedArchiver.archiveRootObject(arrayToSave, toFile: directoryName)
}
func loadArrayFrom(_ directoryName: String ) -> NSArray? {
let result = NSKeyedUnarchiver.unarchiveObject(withFile: directoryName)
return result as? NSArray
}
You cannot encode a Swift struct out of the box, you have to add a computed property and an
init
method to make the struct property list compliantNow you can archive the array
and unarchive it
Alternatively leave
StateObject
unchanged andin
encode(with
replace the linewith
in
init(coder
replace the linewith
Notes:
Since you are assigning default values to all properties you can delete the entire
init()
method and theinit()
call ininit(coder
.Don't use
NSArray
in Swift. Use a nativeArray
It's not a good idea to archive an UI element like
UIImageView
. Archive the image data.