-
Notifications
You must be signed in to change notification settings - Fork 362
Open
Description
I would like to support saving integer to keychain.
would this be ok?
it seems working under iOS but i would like to be sure there nothing wrong:
extension KeychainSwift {
func getInt(_ key: String) -> Int? {
if let data = getData(key) {
do {
let number = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSNumber.self, from: data)
return number?.intValue
} catch {
print("Unarchiving error: \(error)")
return nil
}
} else {
return nil
}
}
@discardableResult
func set(_ value: Int, forKey key: String) -> Bool {
let number = NSNumber(value: value)
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: number, requiringSecureCoding: true)
return set(data, forKey: key)
} catch {
print("Archiving error: \(error)")
return false
}
}
}
And i would use it as:
let keychain = KeychainSwift()
keychain.set(1234, forKey: "test")
let testNumber = keychain.getInt("test")
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels