Как сохранить и заархивировать координату 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', Почему я получаю эту ошибку? Как мне это исправить? Спасибо!

2 ответа

Как говорит Мэтт, используйте NSValue обернуть вашу координату. Похоже, есть готовая оболочка NSValue для координаты:

Посмотрите на NSValue инициализатор

init(mkCoordinate:)

Это позволяет вам преобразовать CLCoordinate2D для NSValue, который вы можете записать непосредственно в архив.

Чтобы использовать 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
Другие вопросы по тегам