【发布时间】:2015-10-22 07:13:01
【问题描述】:
这是我的课:
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"] 中必须是这样的:
"points": [
{"lat": 47424212, "lon": 8855883},
{"lat": $lat2, "lon": $lon2}
]
谢谢!
【问题讨论】: