在我創建的應用程序中,用戶在地圖上放置一個別針,並將其保存。引腳的座標保存爲CLLocationCoordinate2D
座標。這裏是我的代碼 -如何保存和歸檔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'
錯誤。爲什麼我得到這個錯誤?我如何解決它?謝謝!
什麼參數獲取itemData函數? –
你要保存哪裏? – ppalancica