如何为 iOS 应用程序进行永久存储?
How to make permanent storage for an iOS app?
我正在开发一个应用程序,用户可以在其中 select 地图上的建筑物并将它们保存在列表中。一切都很好,但我需要在应用关闭后保存用户 selection。
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!
}
您可以将建筑物 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
我正在开发一个应用程序,用户可以在其中 select 地图上的建筑物并将它们保存在列表中。一切都很好,但我需要在应用关闭后保存用户 selection。
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!
}
您可以将建筑物 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