This is my class:
public class Coordinate: NSObject{
public var lat: Double
public var lon: Double
init(latitude: Double, longitude: Double) {
self.lat = latitude
self.lon = longitude
}
init(coder aDecoder: NSCoder!) {
self.lat = aDecoder.decodeObjectForKey("latitude") as! Double
self.lon = aDecoder.decodeObjectForKey("longitude") as! Double
}
func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeObject(lat, forKey: "latitude")
aCoder.encodeObject(lon, forKey: "longitude")
}
and thats the way I would get an string of my Array of Object:
var endlist = [Coordinate]()
let data = try NSJSONSerialization.dataWithJSONObject(endlist , options: options)
let string = NSString(data: data, encoding: NSUTF8StringEncoding)
json["points"] = string
And now I get this Error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Test.Coordinate)'
Can someone please help me with this problem?
So in json["points"] must be this:
"points": [
{"lat": 47424212, "lon": 8855883},
{"lat": $lat2, "lon": $lon2}
]
THANKS!