this is a class Place
I defined:
class Place: NSObject {
var latitude: Double
var longitude: Double
init(lat: Double, lng: Double, name: String){
self.latitude = lat
self.longitude = lng
}
required init(coder aDecoder: NSCoder) {
self.latitude = aDecoder.decodeDoubleForKey("latitude")
self.longitude = aDecoder.decodeDoubleForKey("longitude")
}
func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeObject(latitude, forKey: "latitude")
aCoder.encodeObject(longitude, forKey: "longitude")
}
}
This is how I tried to save an array of Place
s:
var placesArray = [Place]
//...
func savePlaces() {
NSUserDefaults.standardUserDefaults().setObject(placesArray, forKey: "places")
println("place saved")
}
It didn't work, this is what I get on the console:
Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')
I am new to iOS, could you help me ?
SECOND EDITION
I found a solution to save the data :
func savePlaces(){
let myData = NSKeyedArchiver.archivedDataWithRootObject(placesArray)
NSUserDefaults.standardUserDefaults().setObject(myData, forKey: "places")
println("place saved")
}
But I get an error when loading the data with this code :
let placesData = NSUserDefaults.standardUserDefaults().objectForKey("places") as? NSData
if placesData != nil {
placesArray = NSKeyedUnarchiver.unarchiveObjectWithData(placesData!) as [Place]
}
the error is :
[NSKeyedUnarchiver decodeDoubleForKey:]: value for key (latitude) is not a double number'
I am pretty sure I archived a Double, there is an issue with the saving/loading process
Any clue ?