如何为iOS应用程序制作永久存储空间?

时间:2016-12-05 14:33:59

标签: ios swift storage

我正在做一个应用程序,用户可以在地图上选择建筑物并将其保存在列表中。一切都很好,但我需要在关闭应用后保存用户选择。

func action(_ gestureRecognizer:UIGestureRecognizer) { // GESTURE RECOGNIZER FUNCTION

    if gestureRecognizer.state == UIGestureRecognizerState.began {

        let touchPoint = gestureRecognizer.location(in: self.map)

        let newCoordinate = self.map.convert(touchPoint, toCoordinateFrom: self.map)

        let location = CLLocation(latitude: newCoordinate.latitude, longitude: newCoordinate.longitude)

        CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in


            var title = ""

            if (error == nil) {

                if let p = placemarks?[0] {


                    var subThoroughfare:String = ""
                    var thoroughfare:String = ""

                    if p.subThoroughfare != nil {

                        subThoroughfare = p.subThoroughfare!

                    }

                    if p.thoroughfare != nil {

                        thoroughfare = p.thoroughfare!

                    }

1 个答案:

答案 0 :(得分:0)

您可以将建筑物ID保存到阵列中,并将该阵列保存到UserDefaults中。

UserDefaults.standard.set(value: buildingIds, forKey: "myBuildingIds")

当您打开应用时,只需询问:

if let myBuildingArrays = UserDefaults.standard.value(forKey: "myBuildingIds"){
//And here you iterate each value of the array to load them into your list.
}

更多信息:https://developer.apple.com/reference/foundation/userdefaults