在我创建的应用中,用户在地图上放置一个图钉并保存。引脚的坐标保存为CLLocationCoordinate2D
坐标。这是我的代码 -
@IBAction func saveItem(_ sender: AnyObject?) {
let itemName = itemNameTextField.text!
let itemDescription = itemDescriptionLabel.text!
let itemLocation = itemLocationTextView.text!
let point = dropPin.coordinate
if itemName == "" {
let alertController = UIAlertController(title: "The Item Name!", message:"You have not entered an item name. Please enter a name before saving.", preferredStyle: UIAlertControllerStyle.alert)
let OKAction = UIAlertAction(title: "Got It!", style: UIAlertActionStyle.default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
if itemDescription == "" {
let alertController = UIAlertController(title: "The Description!", message:"You have not entered a description for your item. Please enter a description before saving.", preferredStyle: UIAlertControllerStyle.alert)
let OKAction = UIAlertAction(title: "Got It!", style: UIAlertActionStyle.default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
if itemLocation == "" {
let alertController = UIAlertController(title: "The Item Location!", message:"You have not entered the location of your item. Please do so before saving. Marking the loction on teh map is not necessary, but it is recommended.", preferredStyle: UIAlertControllerStyle.alert)
let OKAction = UIAlertAction(title: "Got It!", style: UIAlertActionStyle.default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
else{
item = itemData(itemName:itemName, itemDescription:itemDescription, itemPlace:itemLocation, mapPoint:point)
print("Item name: \(itemName), Item Description: \(itemDescription), Item Location: \(itemLocation)")
self.performSegue(withIdentifier: "saveUnwind", sender: self)
}
}
然而,当用户保存时,我收到类似这样的错误 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x170c552d0'
。为什么我收到此错误?我如何解决它?谢谢!
答案 0 :(得分:0)
正如马特所说,用NSValue
来包裹你的坐标。
看起来有一个现成的NSValue包装器用于坐标:
查看NSValue
初始化程序
init(mkCoordinate:)
这使您可以将CLCoordinate2D
转换为NSValue
,然后您可以直接将其写入存档。
答案 1 :(得分:-1)
要使用NSCoding,您必须符合NSObject和NSCoding 例如:
import CoreLocation
class LocationCoordinateWrapper: NSObject, NSCoding {
var coordinate: CLLocationCoordinate2D?
init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
}
required init?(coder aDecoder: NSCoder) {
let lat = aDecoder.decodeDouble(forKey: "lat")
let lon = aDecoder.decodeDouble(forKey: "lon")
coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
}
public func encode(with aCoder: NSCoder) {
if let lat = coordinate?.latitude, let lon = coordinate?.longitude {
aCoder.encode(lat, forKey: "lat")
aCoder.encode(lon, forKey: "lon")
}
}
}
let wrapper = LocationCoordinateWrapper(coordinate: CLLocationCoordinate2D(latitude: 14.0, longitude: -71.0))
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let filePath = documentsPath.appending("/sample_filename.loc")
NSKeyedArchiver.archiveRootObject(wrapper, toFile: filePath) // saves to a file
let loaded = NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? LocationCoordinateWrapper // loads back in the file