Please ignore this reply. I found the solution myself, which was to use decodeDouble() instead of decodeObject() to decode location co-ordinates.
Actually, I'm still stuck 😕
So here's what I have now:
The basic object that I want to save an array of looks like this ...
class Site: NSObject, NSCoding, MKAnnotation
{
var coordinate = CLLocationCoordinate2D(latitude:0, longitude:0)
var fOwnerID = ""
var fIcon = ""
var fDate = Date()
override init()
{
}
required convenience init(coder aDecoder: NSCoder)
{
let latitude = aDecoder.decodeObject(forKey: "latitude") as? CLLocationDegrees
let longitude = aDecoder.decodeObject(forKey: "longitude") as? CLLocationDegrees
let owner = aDecoder.decodeObject(forKey: "fOwnerID") as? String
let icon = aDecoder.decodeObject(forKey: "fIcon") as? String
let dtim = aDecoder.decodeObject(forKey: "fDate") as? Date
self.init()
self.coordinate = CLLocationCoordinate2D(latitude:latitude!, longitude:longitude!)
self.fOwnerID = owner!
self.fIcon = icon!
self.fDate = dtim!
}
func encode(with aCoder: NSCoder)
{
aCoder.encode(coordinate.latitude, forKey: "latitude")
aCoder.encode(coordinate.longitude, forKey: "longitude")
aCoder.encode(fOwnerID, forKey: "fOwnerID")
aCoder.encode(fIcon, forKey: "fIcon")
aCoder.encode(fDate, forKey: "fDate")
}
}
... while the object containing the array encodes it like this ...
aCoder.encode(fSites, forKey: "fSites")
... and decodes it like this ...
self.fSites = (aDecoder.decodeObject(forKey: "fSites") as? [Site])!
... but I get an "unexpectedly found nil while unwrapping an Optional value" when trying to construct the coordinate. According to the documentation, decodeObject only returns nil if either the key or its value is nil, but perhaps the cast to CLLocationDegrees is the problem (?) The latter is required to prevent an error in the initializer for a CLLocationCoordinate2D.
Thanks again in advance for your ongoing assistance with these issues.
Steve.