Skip to content

Instantly share code, notes, and snippets.

@npu3pak
Created February 17, 2017 05:07
Show Gist options
  • Save npu3pak/04e14bf78cfba408699caffb2d25254a to your computer and use it in GitHub Desktop.
Save npu3pak/04e14bf78cfba408699caffb2d25254a to your computer and use it in GitHub Desktop.
Хелпер для работы с настройками на Swift
import Foundation
class PreferencesStorage {
private static let keyStartBusStopId = "StartBusStopId"
var startBusStopId: Int64?
private static let keyEndBusStopId = "EndBusStopId"
var endBusStopId: Int64?
private init() {}
//Загрузка
static func load() -> PreferencesStorage {
let defaults = UserDefaults.standard
let storage = PreferencesStorage()
storage.startBusStopId = defaults.object(forKey: PreferencesStorage.keyStartBusStopId) as? Int64
storage.endBusStopId = defaults.object(forKey: PreferencesStorage.keyEndBusStopId) as? Int64
return storage
}
func save() {
let defaults = UserDefaults.standard
defaults.set(startBusStopId, forKey: PreferencesStorage.keyStartBusStopId)
defaults.set(endBusStopId, forKey: PreferencesStorage.keyEndBusStopId)
defaults.synchronize()
}
func clear() {
startBusStopId = nil
endBusStopId = nil
let defaults = UserDefaults.standard
defaults.removeObject(forKey: PreferencesStorage.keyStartBusStopId)
defaults.removeObject(forKey: PreferencesStorage.keyEndBusStopId)
defaults.synchronize()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment