-1

I am trying to fetch a data from UserDefault but when I am doing this I am getting error

var sharedPreference: UserDefaults = UserDefaults.init(suiteName: "user-key-value")!
func getLastLoginClientId() -> Int64? {

        for (key, value) in sharedPreference.dictionaryRepresentation() {
            if key == LAST_USER {
                return value as! Int64
            }
        }
        return nil
}

I am getting that my key is having some value but when returning it, it throws error.

This is how I save

func setLastLoginClientId(clientId: Int64) {
        sharedPreference.set(clientId, forKey: LAST_USER)
        sharedPreference.synchronize()
    }

1 Answers1

2

I think you could do something as simple as

func getLastLoginClientId() -> Int64? {
    return sharedPreference.value(forKey: LAST_USER) as? Int64
}

Here is what I've tested

struct CustomUserDefaults {

    var sharedPreference    : UserDefaults = UserDefaults.init(suiteName: "user-key-value")!
    let LAST_USER = "test"

    func test() {

        let value = Int64(20.0)
        self.setLastLoginClientId(value)

        let testValue = getLastLoginClientId()
        print(testValue) // 20.0
    }

    func setLastLoginClientId(_ value: Int64) {
        sharedPreference.set(value, forKey: LAST_USER)
    }

    func getLastLoginClientId() -> Int64? {
        return sharedPreference.value(forKey: LAST_USER) as? Int64
    }
}
Ben
  • 1,414
  • 13
  • 23
  • That is not working. It is returning nil thats why I changed it to that way – Shubham Agarwal Bhewanewala Jan 31 '19 at 10:04
  • Can you elaborate a bit what is not working? I've updated with a code I tried that is working fine. – Ben Jan 31 '19 at 10:07
  • That's 100% correct and it should work like this only but Here it is not happening. It is always returning nil to me in this case only. All other of these kind is returning correct value – Shubham Agarwal Bhewanewala Jan 31 '19 at 10:12
  • Can you check with a breakpoint you are not resetting the value in UserDefaults before accessing it? Although, is it executed in different thread? Maybe you try to access it before being fully set. – Ben Jan 31 '19 at 10:14
  • The variable is reflecting value in debugging and data is already in UserDefaults – Shubham Agarwal Bhewanewala Jan 31 '19 at 10:16
  • I would suggest to eventually delete your app, clean your project and rerun this example. in case you have corrupted data in UserDefault after trying many times. Otherwise it's something else in your code since this one above is working fine. – Ben Jan 31 '19 at 10:20
  • 1
    `synchronize()` does not need to be called anymore. [Documentation](https://developer.apple.com/documentation/foundation/userdefaults) "Waits for any pending asynchronous updates to the defaults database and returns; **this method is unnecessary and shouldn't be used.**" – Scriptable Jan 31 '19 at 10:36
  • @Ben it is working now. I passed it as string for now and typecasted it. Thanx for help. Will b looking in it little later – Shubham Agarwal Bhewanewala Jan 31 '19 at 10:40