evgenyneu / keychain-swift

Helper functions for saving text in Keychain securely for iOS, OS X, tvOS and watchOS.
MIT License
2.85k stars 345 forks source link

Integer support - is this ok? #177

Open DanielZanchi opened 10 months ago

DanielZanchi commented 10 months ago

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 commented 10 months ago

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

brzzdev commented 6 months ago

This would be really useful

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