2016-12-05 118 views
-3

我正在做一個應用程序,用戶可以在地圖上選擇建築物並將它們保存在列表中。一切都很好,但我需要在應用程序關閉後保存用戶選擇。如何爲iOS應用程序創建永久存儲?

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

歡迎堆棧溢出!您可以參考[遊覽](http://stackoverflow.com/tour)並學習[如何問](http://stackoverflow.com/help/how-to-ask)一個很好的問題,並創建一個[Minimal ,完整和可驗證示例](http://stackoverflow.com/help/mcve)。這使我們更容易幫助你。 – Starlord

回答

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