2016-11-21 25 views
0

在我創建的應用程序中,用戶在地圖上放置一個別針,並將其保存。引腳的座標保存爲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'錯誤。爲什麼我得到這個錯誤?我如何解決它?謝謝!

+0

什麼參數獲取itemData函數? –

+0

你要保存哪裏? – ppalancica

回答

-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 
+0

確實沒有意義,既然已經有一個位置座標包裝器,即NSValue。 – matt

+0

我正在使用NSCoding,這基本上就是我所做的。我只是沒有包含該代碼。 –

0

馬特說,使用NSValue來包裝你的座標。 它看起來像有一個現成的NSValue封裝爲一個座標:

看看在NSValue初始化

這可以讓你轉換一個CLCoordinate2DNSValue,然後你就可以直接寫歸檔。