Using enums in a class as a parameter - ios

Basically I have a DataManager singleton as follows:
class DataManager {
static let shared = DataManager()
private init() {}
static func set(_ object: String, forKey defaultName: String) {
let defaults: UserDefaults = UserDefaults.standard
defaults.set(object, forKey:defaultName)
defaults.synchronize()
}
static func object(forKey key: String) -> String {
let defaults: UserDefaults = UserDefaults.standard
return defaults.object(forKey: key) as! String
}
}
What I want to achieve it as follows, when setting or retrieving saved objects:
DataManager.set("some hash", forKey: .hash)
instead of:
DataManager.set("some hash", forKey: "hash")
Also, what's the technical term for the functionality I'm looking for?

You can have an enum, simply DefaultKey
enum DefaultKey: String {
case hash = "hash"
case secondKey = "secondKey"
case thirdKey = "thirdKey"
}
Now what you have to do is
static func set(_ object: String, forKey keyType: DefaultKey) {
let defaults: UserDefaults = UserDefaults.standard
let key = keyType.rawValue // rawvalue for keyType .hash will actually return you "hash".
defaults.set(object, forKey: key)
defaults.synchronize()
}
Now you will be able to call function like
DataManager.set("some hash", forKey: .hash)

Yes, you can use enums, for example:
enum DataManagerKey: String {
case hash
}
and then use it in set method:
static func set(_ object: String, forKey defaultName: DataManagerKey)

You could simply achieve it by implementing an enum and let defaultName parameter data type to be of the implemented enum, as follows:
enum DataManagerDefaultName: String {
case hash
// other cases goes here
}
Usage:
class DataManager {
static let shared = DataManager()
private init() {
}
static func set(_ object: String, forKey defaultName: String) { {
let defaults: UserDefaults = UserDefaults.standard
defaults.set(object, forKey:defaultName.rawValue)
defaults.synchronize()
}
static func object(forKey key: DataManagerDefaultName) -> String {
let defaults: UserDefaults = UserDefaults.standard
return defaults.object(forKey: key) as! String
}
}
Executing:
// executing:
// setting the value:
DataManager.set("my value", forKey: .hash)
// getting the value:
let myValue = DataManager.object(forKey: .hash) // "my value"
Note that you'd be able to not only set the value by using the enum, also getting it.

I find the most clean approach to be defining an enum inside the class, like this:
class DataManager {
enum Key: String {
case hash
}
static let shared = DataManager()
private init() {
}
static func set(_ object: String, forKey defaultName: DataManager.Key) {
let defaults: UserDefaults = UserDefaults.standard
defaults.set(object, forKey:defaultName.rawValue)
defaults.synchronize()
}
static func object(forKey key: DataManager.Key) -> String {
let defaults: UserDefaults = UserDefaults.standard
return defaults.object(forKey: key.rawValue) as! String
}
}
Usage stays the same as if it were defined outside, since Swift will infer the DataManager.Key prefix:
DataManager.set("some hash", forKey: .hash)

Related

How to test and mock property wrappers in Swift?

Let's say I have a very common use case for a property wrapper using UserDefaults.
#propertyWrapper
struct DefaultsStorage<Value> {
private let key: String
private let storage: UserDefaults
var wrappedValue: Value? {
get {
guard let value = storage.value(forKey: key) as? Value else {
return nil
}
return value
}
nonmutating set {
storage.setValue(newValue, forKey: key)
}
}
init(key: String, storage: UserDefaults = .standard) {
self.key = key
self.storage = storage
}
}
I am now declaring an object that would hold all my values stored in UserDefaults.
struct UserDefaultsStorage {
#DefaultsStorage(key: "userName")
var userName: String?
}
Now when I want to use it somewhere, let's say in a view model, I would have something like this.
final class ViewModel {
func getUserName() -> String? {
UserDefaultsStorage().userName
}
}
Few questions arise here.
It seems that I am obliged to use .standard user defaults in this case. How to test that view model using other/mocked instance of UserDefaults?
How to test that property wrapper using other/mocked instance of UserDefaults? Do I have to create a new type that is a clean copy of the above's DefaultsStorage, pass mocked UserDefaults and test that object?
struct TestUserDefaultsStorage {
#DefaultsStorage(key: "userName", storage: UserDefaults(suiteName: #file)!)
var userName: String?
}
As #mat already mentioned in the comments, you need a protocol to mock UserDefaults dependency. Something like this will do:
protocol UserDefaultsStorage {
func value(forKey key: String) -> Any?
func setValue(_ value: Any?, forKey key: String)
}
extension UserDefaults: UserDefaultsStorage {}
Then you can change your DefaultsStorage propertyWrapper to use a UserDefaultsStorage reference instead of UserDefaults:
#propertyWrapper
struct DefaultsStorage<Value> {
private let key: String
private let storage: UserDefaultsStorage
var wrappedValue: Value? {
get {
return storage.value(forKey: key) as? Value
}
nonmutating set {
storage.setValue(newValue, forKey: key)
}
}
init(key: String, storage: UserDefaultsStorage = UserDefaults.standard) {
self.key = key
self.storage = storage
}
}
After that a mock UserDefaultsStorage might look like this:
class UserDefaultsStorageMock: UserDefaultsStorage {
var values: [String: Any]
init(values: [String: Any] = [:]) {
self.values = values
}
func value(forKey key: String) -> Any? {
return values[key]
}
func setValue(_ value: Any?, forKey key: String) {
values[key] = value
}
}
And to test DefaultsStorage, pass an instance of UserDefaultsStorageMock as its storage parameter:
import XCTest
class DefaultsStorageTests: XCTestCase {
class TestUserDefaultsStorage {
#DefaultsStorage(
key: "userName",
storage: UserDefaultsStorageMock(values: ["userName": "TestUsername"])
)
var userName: String?
}
func test_userName() {
let testUserDefaultsStorage = TestUserDefaultsStorage()
XCTAssertEqual(testUserDefaultsStorage.userName, "TestUsername")
}
}
This might not be the best solution, however, I haven't figured out a way to inject UserDefaults that use property wrappers into a ViewModel. If there is such an option, then gcharita's proposal to use another protocol would be a good one to implement.
I used the same UserDefaults in the test class as in the ViewModel. I save the original values before each test and restore them after each test.
class ViewModelTests: XCTestCase {
private lazy var userDefaults = newUserDefaults()
private var preTestsInitialValues: PreTestsInitialValues!
override func setUpWithError() throws {
savePreTestUserDefaults()
}
override func tearDownWithError() throws {
restoreUserDefaults()
}
private func newUserDefaults() -> UserDefaults.Type {
return UserDefaults.self
}
private func savePreTestUserDefaults() {
preTestsInitialValues = PreTestsInitialValues(userName: userDefaults.userName)
}
private func restoreUserDefaults() {
userDefaults.userName = preTestsInitialValues.userName
}
func testUsername() throws {
//"inject" User Defaults with the desired values
let username = "No one"
userDefaults.userName = username
let viewModel = ViewModel()
let usernameFromViewModel = viewModel.getUserName()
XCTAssertEqual(username, usernameFromViewModel)
}
}
struct PreTestsInitialValues {
let userName: String?
}

Property Wrappers With Generic (Optional) User Defaults

With due reference:
https://github.com/apple/swift-evolution/blob/master/proposals/0258-property-wrappers.md#user-defaults
We've started to use property wrappers for the UserDefaults, it works seamlessly with non-optional properties.
However, setting nil of an optional property crashes with:
[User Defaults] Attempt to set a non-property-list object as an
NSUserDefaults/CFPreferences value for key "someKeyThatWeSet"
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'Attempt to insert non-property
list object null for key "someKeyThatWeSet"'
The code below can be tested on Playground directly:
#propertyWrapper
struct C2AppProperty<T> {
let key: String
let defaultValue: T
init(_ key: String, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
var wrappedValue: T {
get {
return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue
}
set {
UserDefaults.standard.set(newValue, forKey: key)
}
}
}
struct C2User {
#C2AppProperty("userID", defaultValue: nil)
public static var publicUserID: String?
}
print(C2User.publicUserID)
C2User.publicUserID = "edusta"
print(C2User.publicUserID)
C2User.publicUserID = nil
print(C2User.publicUserID)
Expected:
nil
Optional<"edusta">
nil
Found:
nil
Optional<"edusta">
libc++abi.dylib: terminating with uncaught exception of type
NSException
What I've tried so far:
set {
// Comparing non-optional value of type 'T' to nil always returns false.
if newValue == nil {
UserDefaults.standard.removeObject(forKey: combinedKey)
} else {
UserDefaults.standard.set(newValue, forKey: combinedKey)
}
}
What kind of a check is needed here to catch that newValue is nil? Or an Optional<nil>?
This code works for me:
#propertyWrapper
struct UserDefault<T> {
private let key: String
private let defaultValue: T
private let userDefaults: UserDefaults
init(_ key: String, defaultValue: T, userDefaults: UserDefaults = .standard) {
self.key = key
self.defaultValue = defaultValue
self.userDefaults = userDefaults
}
var wrappedValue: T {
get {
guard let value = userDefaults.object(forKey: key) else {
return defaultValue
}
return value as? T ?? defaultValue
}
set {
if let value = newValue as? OptionalProtocol, value.isNil() {
userDefaults.removeObject(forKey: key)
} else {
userDefaults.set(newValue, forKey: key)
}
}
}
}
fileprivate protocol OptionalProtocol {
func isNil() -> Bool
}
extension Optional : OptionalProtocol {
func isNil() -> Bool {
return self == nil
}
}
Thanks to Michcio for providing the solution,
If we read property very frequently, the following code may have better performance. Use storage property can save value in memory, it won't be read value from disk every time.
#propertyWrapper
struct ReadOptimizeStorage<T> {
private let key: String
private let defaultValue: T
private let userDefaults: UserDefaults
init(key: String, defaultValue: T, userDefaults: UserDefaults = .standard) {
self.key = key
self.defaultValue = defaultValue
self.userDefaults = userDefaults
wrappedValue = userDefaults.object(forKey: key) as? T ?? defaultValue
}
var wrappedValue: T {
didSet {
if let value = wrappedValue as? OptionalProtocol, value.isNil() {
userDefaults.removeObject(forKey: key)
} else {
userDefaults.set(wrappedValue, forKey: key)
}
}
}
}
fileprivate protocol OptionalProtocol {
func isNil() -> Bool
}
extension Optional : OptionalProtocol {
func isNil() -> Bool {
return self == nil
}
}
I made a tiny spm package inspired by the ripples answers above:
https://github.com/mezhevikin/userdefault.git
import UserDefault
extension UserDefaults {
// Optional property
#UserDefault("login", .standard)
static var login: String?
// Property with default value
#UserDefault("isDarkMode", false)
static var isDarkMode: Bool
}
UserDefaults.login = "mezhevikin"
UserDefaults.isDarkMode = true

How to store a dictionary [String:[Object]]?

So I have a class called Restaurant and a [String:[Restaurant]] dictionary. How should I properly store this dictionary so that data inside it wouldn't be lost after user relaunches the app?
class Restaurant {
var name: String?
init?(name: String) {
guard !name.isEmpty else {
return nil
}
self.name = name
}
}
var restaurants = [String: [Restaurant]]()
override func viewDidLoad() {
super.viewDidLoad()
// ...
restaurants["Pizza"] = ...
}
Try using realm.io
If you want to conform both decodable and the realm object to create the object in disk you could do it as follow
import RealmSwift
class Restaurant: Object, Decodable {
#objc dynamic var name: String?
private enum CodingKeys: String, CodingKey { case name}
convenience init( name: String?){
self.init()
self.name = name
}
convenience required init(from decoder: Decoder) throws {
self.init()
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
}
}
func saveToDisk(){
let realm = try! Realm()
try! realm.write {
realm.add(restaurant)
}
}
You can adapt it to save an array of Restaurant Object
You can use other different methods as:
CoreData
Using a file
UserDefaults(too slow and not for large amount of data)

Save Struct to UserDefaults

I have a struct that I want to save to UserDefaults. Here's my struct
struct Song {
var title: String
var artist: String
}
var songs: [Song] = [
Song(title: "Title 1", artist "Artist 1"),
Song(title: "Title 2", artist "Artist 2"),
Song(title: "Title 3", artist "Artist 3"),
]
In another ViewController, I have a UIButton that appends to this struct like
#IBAction func likeButtonPressed(_ sender: Any) {
songs.append(Song(title: songs[thisSong].title, artist: songs[thisSong].artist))
}
I want it so that whenever the user clicks on that button also, it saves the struct to UserDefaults so that whenever the user quits the app and then opens it agian, it is saved. How would I do this?
In Swift 4 this is pretty much trivial. Make your struct codable simply by marking it as adopting the Codable protocol:
struct Song:Codable {
var title: String
var artist: String
}
Now let's start with some data:
var songs: [Song] = [
Song(title: "Title 1", artist: "Artist 1"),
Song(title: "Title 2", artist: "Artist 2"),
Song(title: "Title 3", artist: "Artist 3"),
]
Here's how to get that into UserDefaults:
UserDefaults.standard.set(try? PropertyListEncoder().encode(songs), forKey:"songs")
And here's how to get it back out again later:
if let data = UserDefaults.standard.value(forKey:"songs") as? Data {
let songs2 = try? PropertyListDecoder().decode(Array<Song>.self, from: data)
}
This is my UserDefaults extension in main thread, to set get Codable object into UserDefaults
// MARK: - UserDefaults extensions
public extension UserDefaults {
/// Set Codable object into UserDefaults
///
/// - Parameters:
/// - object: Codable Object
/// - forKey: Key string
/// - Throws: UserDefaults Error
public func set<T: Codable>(object: T, forKey: String) throws {
let jsonData = try JSONEncoder().encode(object)
set(jsonData, forKey: forKey)
}
/// Get Codable object into UserDefaults
///
/// - Parameters:
/// - object: Codable Object
/// - forKey: Key string
/// - Throws: UserDefaults Error
public func get<T: Codable>(objectType: T.Type, forKey: String) throws -> T? {
guard let result = value(forKey: forKey) as? Data else {
return nil
}
return try JSONDecoder().decode(objectType, from: result)
}
}
Update This is my UserDefaults extension in background, to set get Codable object into UserDefaults
// MARK: - JSONDecoder extensions
public extension JSONDecoder {
/// Decode an object, decoded from a JSON object.
///
/// - Parameter data: JSON object Data
/// - Returns: Decodable object
public func decode<T: Decodable>(from data: Data?) -> T? {
guard let data = data else {
return nil
}
return try? self.decode(T.self, from: data)
}
/// Decode an object in background thread, decoded from a JSON object.
///
/// - Parameters:
/// - data: JSON object Data
/// - onDecode: Decodable object
public func decodeInBackground<T: Decodable>(from data: Data?, onDecode: #escaping (T?) -> Void) {
DispatchQueue.global().async {
let decoded: T? = self.decode(from: data)
DispatchQueue.main.async {
onDecode(decoded)
}
}
}
}
// MARK: - JSONEncoder extensions
public extension JSONEncoder {
/// Encodable an object
///
/// - Parameter value: Encodable Object
/// - Returns: Data encode or nil
public func encode<T: Encodable>(from value: T?) -> Data? {
guard let value = value else {
return nil
}
return try? self.encode(value)
}
/// Encodable an object in background thread
///
/// - Parameters:
/// - encodableObject: Encodable Object
/// - onEncode: Data encode or nil
public func encodeInBackground<T: Encodable>(from encodableObject: T?, onEncode: #escaping (Data?) -> Void) {
DispatchQueue.global().async {
let encode = self.encode(from: encodableObject)
DispatchQueue.main.async {
onEncode(encode)
}
}
}
}
// MARK: - NSUserDefaults extensions
public extension UserDefaults {
/// Set Encodable object in UserDefaults
///
/// - Parameters:
/// - type: Encodable object type
/// - key: UserDefaults key
/// - Throws: An error if any value throws an error during encoding.
public func set<T: Encodable>(object type: T, for key: String, onEncode: #escaping (Bool) -> Void) throws {
JSONEncoder().encodeInBackground(from: type) { [weak self] (data) in
guard let data = data, let `self` = self else {
onEncode(false)
return
}
self.set(data, forKey: key)
onEncode(true)
}
}
/// Get Decodable object in UserDefaults
///
/// - Parameters:
/// - objectType: Decodable object type
/// - forKey: UserDefaults key
/// - onDecode: Codable object
public func get<T: Decodable>(object type: T.Type, for key: String, onDecode: #escaping (T?) -> Void) {
let data = value(forKey: key) as? Data
JSONDecoder().decodeInBackground(from: data, onDecode: onDecode)
}
}
If the struct contains only property list compliant properties I recommend to add a property propertyListRepresentation and a corresponding init method
struct Song {
var title: String
var artist: String
init(title : String, artist : String) {
self.title = title
self.artist = artist
}
init?(dictionary : [String:String]) {
guard let title = dictionary["title"],
let artist = dictionary["artist"] else { return nil }
self.init(title: title, artist: artist)
}
var propertyListRepresentation : [String:String] {
return ["title" : title, "artist" : artist]
}
}
To save an array of songs to UserDefaults write
let propertylistSongs = songs.map{ $0.propertyListRepresentation }
UserDefaults.standard.set(propertylistSongs, forKey: "songs")
To read the array
if let propertylistSongs = UserDefaults.standard.array(forKey: "songs") as? [[String:String]] {
songs = propertylistSongs.flatMap{ Song(dictionary: $0) }
}
If title and artist will never be mutated consider to declare the properties as constants (let) .
This answer was written while Swift 4 was in beta status. Meanwhile conforming to Codable is the better solution.
Here is a modern Swift 5.1 #propertyWrapper, allowing to store any Codable object in form of a human readable JSON string:
#propertyWrapper struct UserDefaultEncoded<T: Codable> {
let key: String
let defaultValue: T
init(key: String, default: T) {
self.key = key
defaultValue = `default`
}
var wrappedValue: T {
get {
guard let jsonString = UserDefaults.standard.string(forKey: key) else {
return defaultValue
}
guard let jsonData = jsonString.data(using: .utf8) else {
return defaultValue
}
guard let value = try? JSONDecoder().decode(T.self, from: jsonData) else {
return defaultValue
}
return value
}
set {
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
guard let jsonData = try? encoder.encode(newValue) else { return }
let jsonString = String(bytes: jsonData, encoding: .utf8)
UserDefaults.standard.set(jsonString, forKey: key)
}
}
}
Usage:
extension Song: Codable {}
#UserDefaultEncoded(key: "songs", default: [])
var songs: [Song]
func addSong(_ song: Song) {
// This will automatically store new `songs` value
// to UserDefaults
songs.append(song)
}
From here:
A default object must be a property list—that is, an instance of (or for collections, a combination of instances of):
NSData
,
NSString
,
NSNumber
,
NSDate
,
NSArray
, or
NSDictionary
. If you want to store any other type of object, you should typically archive it to create an instance of NSData.
You need to use NSKeydArchiver. Documentation can be found here and examples here and here.
If you are just trying to save this array of songs in UserDefaults and nothing fancy use this:-
//stores the array to defaults
UserDefaults.standard.setValue(value: songs, forKey: "yourKey")
//retrieving the array
UserDefaults.standard.object(forKey: "yourKey") as! [Song]
//Make sure to typecast this as an array of Song
If you are storing a heavy array, I suggest you to go with NSCoding protocol or the Codable Protocol in swift 4
Example of coding protocol:-
struct Song {
var title: String
var artist: String
}
class customClass: NSObject, NSCoding { //conform to nsobject and nscoding
var songs: [Song] = [
Song(title: "Title 1", artist "Artist 1"),
Song(title: "Title 2", artist "Artist 2"),
Song(title: "Title 3", artist "Artist 3"),
]
override init(arr: [Song])
self.songs = arr
}
required convenience init(coder aDecoder: NSCoder) {
//decoding your array
let songs = aDecoder.decodeObject(forKey: "yourKey") as! [Song]
self.init(are: songs)
}
func encode(with aCoder: NSCoder) {
//encoding
aCoder.encode(songs, forKey: "yourKey")
}
}
I'd imagine that it should be quite common to represent a user's settings as an observable object. So, here's an example of keeping observable data synchronised with user defaults and updated for xCode 11.4. This can be used in the context of environment objects also.
import SwiftUI
final class UserData: ObservableObject {
#Published var selectedAddress: String? {
willSet {
UserDefaults.standard.set(newValue, forKey: Keys.selectedAddressKey)
}
}
init() {
selectedAddress = UserDefaults.standard.string(forKey: Keys.selectedAddressKey)
}
private struct Keys {
static let selectedAddressKey = "SelectedAddress"
}
}
Swift 5
If you want need to save struct in UserDefault using only on data format.
Smaple struct
struct StudentData:Codable{
var id: Int?
var name: String?
var createdDate: String?
// for decode the value
init(from decoder: Decoder) throws {
let values = try? decoder.container(keyedBy: codingKeys.self)
id = try? values?.decodeIfPresent(Int.self, forKey: .id)
name = try? values?.decodeIfPresent(String.self, forKey: .name)
createdDate = try? values?.decodeIfPresent(String.self, forKey: .createdDate)
}
// for encode the value
func encode(to encoder: Encoder) throws {
var values = encoder.container(keyedBy: codingKeys.self)
try? values.encodeIfPresent(id, forKey: .id)
try? values.encodeIfPresent(name, forKey: .name)
try? values.encodeIfPresent(createdDate, forKey: .createdDate)
}
}
There are two types to convert as data
Codable (Encodable and Decodable).
PropertyListEncoder and PropertyListDecoder
First we using the Codable (Encodable and Decodable) to save the struct
Example for save value
let value = StudentData(id: 1, name: "Abishek", createdDate: "2020-02-11T11:23:02.3332Z")
guard let data = try? JSONEncoder().encode(value) else {
fatalError("unable encode as data")
}
UserDefaults.standard.set(data, forKey: "Top_student_record")
Retrieve value
guard let data = UserDefaults.standard.data(forKey: "Top_student_record") else {
// write your code as per your requirement
return
}
guard let value = try? JSONDecoder().decode(StudentData.self, from: data) else {
fatalError("unable to decode this data")
}
print(value)
Now we using the PropertyListEncoder and PropertyListDecoder to save the struct
Example for save value
let value = StudentData(id: 1, name: "Abishek", createdDate: "2020-02-11T11:23:02.3332Z")
guard let data = try? PropertyListEncoder().encode(value) else {
fatalError("unable encode as data")
}
UserDefaults.standard.set(data, forKey: "Top_student_record")
Retrieve value
guard let data = UserDefaults.standard.data(forKey: "Top_student_record") else {
// write your code as per your requirement
return
}
guard let value = try? PropertyListDecoder().decode(StudentData.self, from: data) else {
fatalError("unable to decode this data")
}
print(value)
In your convenience you can use the any type to save the struct in userDefault.
Here is a simpler solution
#propertyWrapper
struct CodableUserDefault<Value: Codable> {
let key: String
let defaultValue: Value
private let container: UserDefaults = .standard
var wrappedValue: Value {
get {
guard let data = container.data(forKey: key), let object = try? JSONDecoder().decode(Value.self, from: data) else {
return defaultValue
}
return object
}
set {
container.set(try? JSONEncoder().encode(newValue), forKey: key)
}
}
}
Usage
enum ACodableEnum: String, Codable {
case first
case second
}
class SomeController {
#CodableUserDefault<ACodableEnum>(key: "key", defaultValue: .first)
private var aCodableEnum: ACodableEnum
}

return token from NSUserDefaults

Here is my code in a swift file to keep token in user device:
struct DefaultsKeys
{
static let token = ""
}
class DataContainerSingleton
{
static let sharedDataContainer = DataContainerSingleton()
var token: String?
var goToBackgroundObserver: AnyObject?
init()
{
let defaults = UserDefaults.standard
token = defaults.object(forKey: DefaultsKeys.token) as! String?
goToBackgroundObserver = NotificationCenter.default.addObserver(
forName: NSNotification.Name.UIApplicationDidEnterBackground,
object: nil,
queue: nil)
{
(note: Notification!) -> Void in
let defaults = UserDefaults.standard
defaults.set( self.token, forKey: DefaultsKeys.token)
defaults.synchronize()
}
}
}
Then when I get the token from Alamofire request I save it as :
DataContainerSingleton.sharedDataContainer.token = usertoken
but when I go to viewdidload and check if DataContainerSingleton.sharedDataContainer.token != nil it return nil after I re-run the project! even when I print DataContainerSingleton.sharedDataContainer.token it prints nil!
Where is my mistake pleas
Edit:
here is what I've tried but it returns nil #Paulw11
`struct DefaultsKeys
{
static let token = "token"
}
class DataContainerSingleton
{
static let sharedDataContainer = DataContainerSingleton()
var token: String? {
didSet {
let defaults = UserDefaults.standard
defaults.set(token, forKey:DefaultsKeys.token)
defaults.synchronize()
}
}
}`
Why are you using all of this complicated logic? Saving to NSUserDefaults is low impact; you should simply save it in a didSet for the token property. Also, make sure you load the value back when you initialise your class.
class DataContainerSingleton
{
static let sharedDataContainer = DataContainerSingleton()
var token: String? {
didSet {
let defaults = UserDefaults.standard
defaults.set(token, forKey:DefaultsKeys.token)
}
}
init() {
let defaults = UserDefaults.standard
self.token = defaults.object(forKey:DefaultsKeys.token) as? String
}
}
Also, you need to provide a key name for your default:
struct DefaultsKeys
{
static let token = "token"
}

Resources