1
這是我的課:雨燕2.0 Array對象到JSON字符串
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")
}
和那我會得到我的對象數組的字符串方式:
var endlist = [Coordinate]()
let data = try NSJSONSerialization.dataWithJSONObject(endlist , options: options)
let string = NSString(data: data, encoding: NSUTF8StringEncoding)
json["points"] = string
現在我得到這個錯誤:
終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: '在JSON寫入無效型(Test.Coordinate)'
有人可以幫我解決這個問題嗎?
所以在JSON [ 「點」]必須是這樣的:
"points": [
{"lat": 47424212, "lon": 8855883},
{"lat": $lat2, "lon": $lon2}
]
謝謝!