vpeschenkov / SecureDefaults

Elevate the security of your UserDefaults with this lightweight wrapper that adds a layer of AES-256 encryption
MIT License
227 stars 17 forks source link

When accessing app group lots of errors about optionals #24

Open poml88 opened 2 months ago

poml88 commented 2 months ago

Hi, if I use this code in Xcode 15

let sdefaults = SecureDefaults(suiteName: "a")
        if !sdefaults.isKeyCreated {
            sdefaults.password = UUID().uuidString
        }
        sdefaults.set(password, forKey: "libre-direct.settings.password")
        sdefaults.synchronize()

I get quite some errors about optionals:

Value of optional type 'SecureDefaults?' must be unwrapped to refer to member 'isKeyCreated' of wrapped base type 'SecureDefaults'
Value of optional type 'SecureDefaults?' must be unwrapped to refer to member 'password' of wrapped base type 'SecureDefaults'
Value of optional type 'SecureDefaults?' must be unwrapped to refer to member 'set' of wrapped base type 'SecureDefaults'
Value of optional type 'SecureDefaults?' must be unwrapped to refer to member 'synchronize' of wrapped base type 'SecureDefaults'
poml88 commented 2 months ago

When I use this extension:

extension SecureDefaults {
    static let sgroup = SecureDefaults(suiteName: stringSValue(forKey: "APP_GROUP_ID"))!

    static func stringSValue(forKey key: String) -> String {
        guard let value = Bundle.main.object(forInfoDictionaryKey: key) as? String else {
            fatalError("Invalid value or undefined key")
        }
        return value
    }
}

it does work.