Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer support - is this ok? #177

Open
DanielZanchi opened this issue Jan 9, 2024 · 2 comments
Open

Integer support - is this ok? #177

DanielZanchi opened this issue Jan 9, 2024 · 2 comments

Comments

@DanielZanchi
Copy link

DanielZanchi commented Jan 9, 2024

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")
@evgenyneu
Copy link
Owner

Sorry I don't want to add any new features to the library.

@brzzdev
Copy link

brzzdev commented May 25, 2024

This would be really useful

@evgenyneu May I ask why you don't want to add new features?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants