Encode complex object swift 3.0

411 views Asked by At

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
    }
1

There are 1 answers

1
vadian On BEST ANSWER

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 compliant

struct StateObject {
    var isAdd = true
    var path  = ""

    init(propertyList : [String:Any]) {
        self.isAdd = propertyList["isAdd"] as! Bool
        self.path = propertyList["path"] as! String
    }

    var propertyListRepresentation : [String:Any] {
        return ["isAdd" : isAdd, "path"  : path]
    }
}

Now you can archive the array

archiver.encode(self.arrStates.map{$0.propertyListRepresentation}, forKey: ArchiverKeys.stateArray.rawValue)

and unarchive it

let states = unarchiver.decodeObject(forKey: ArchiverKeys.stateArray.rawValue) as! [[String:Any]]
self.arrStates = states.map { StateObject(propertyList: $0) }

Alternatively leave StateObject unchanged and

  • in encode(with replace the line

    archiver.encode(self.arrStates, forKey: ArchiverKeys.stateArray.rawValue)
    

    with

    let arrStatesAsPlist = arrStates.map { return ["isAdd" : $0.isAdd, "path" : $0.path] }
    archiver.encode(arrStatesAsPlist, forKey:ArchiverKeys.stateArray.rawValue)
    
  • in init(coder replace the line

    archiver.encode(self.arrStates, forKey: ArchiverKeys.stateArray.rawValue)
    

    with

    let arrStatesAsPlist = unarchiver.decodeObject(forKey: ArchiverKeys.stateArray.rawValue) as! [[String:Any]]
    arrStates = arrStatesAsPlist.map { StateObject(isAdd: $0["isAdd"] as! Bool, path: $0["path"] as! String) }
    

Notes:

  • Since you are assigning default values to all properties you can delete the entire init() method and the init() call in init(coder.

  • Don't use NSArray in Swift. Use a native Array

  • It's not a good idea to archive an UI element like UIImageView. Archive the image data.