I’ve a struct that contains some CGPoint & array of tuple:
typealias MapNode = (color: UIColor, location: CGPoint)
struct MapCollectionNode {
var position: CGPoint
var containedLocations: [MapNode]
}
Then I’ve an Array consists of MapCollectionNode:
var nodeCollection = [MapCollectionNode]()
Since NSCoding doesn’t support struct and tuple, I had to first break down the nodeCollection as following:
var position = [CGPoint]()
var nodes = [[MapNode]]()
for n in nodeCollection {
position.append(n.position)
nodes.append(n.containedLocations)
}
then I had to further break down the tuples within nodes as following:
var color = [SKColor]()
var location = [CGPoint]()
for i in 0..<nodes.count {
for j in nodes[i] {
color.append(j.color)
location.append(j.location)
}
}
At this point I’m able to encode all the information I needed:
aCoder.encode(position, forKey: "MapClass.position")
aCoder.encode(color, forKey: "MapClass.color")
aCoder.encode(location, forKey: "MapClass.location")
At the time of decoding, I’ve them all decoded as following:
let position = aDecoder.decodeObject(forKey: "MapClass.position") as? [CGPoint]
let color = aDecoder.decodeObject(forKey: "MapClass.color") as? [SKColor]
let location = aDecoder.decodeObject(forKey: "MapClass.location") as? [CGPoint]
However, I’m stuck on how to put them back into variable nodeCollection which is an Array of MapCollectionNode.
Any help would be appreciated.