sunshinejr / SwiftyUserDefaults

Modern Swift API for NSUserDefaults
http://radex.io/swift/nsuserdefaults/static
MIT License
4.85k stars 365 forks source link

Enum issue: 'subscript(dynamicMember:)' is unavailable #226

Closed Kalzem closed 4 years ago

Kalzem commented 4 years ago

I am trying to use an enum which is declared like this:

enum Lang: String, CaseIterable, Codable, DefaultsSerializable {
    case en
    case fr
    case ru
}

And this is how I implemented SwiftyUserDefaults

public struct DefaultsKeys: DefaultsKeyStore {
    // This doesn't work (enum)
    var currentTargetLang: DefaultsKey<Lang?> { return .init("learningLang") }

    // This works (string)
    var enVoiceIdentifier: DefaultsKey<String?> { return .init("enVoiceIdentifier") }
}

// Actually call
var Defaults = DefaultsAdapter<DefaultsKeys>(defaults: UserDefaults(suiteName: appGroupName)!, keyStore: .init())

Defaults.enVoiceIdentifier // works
Defaults.currentTargetLang // doesn't work
image

How am I supposed to use enum with DefaultsKey?

sunshinejr commented 4 years ago

Hey @Kalzem. This is a problem when you have an enum that is both Codable and RawRepresentable (String in your case). See my answer in this issue and let me know if it helps.

Kalzem commented 4 years ago

Ohhh I couldn't find the ticket you mention as the xcode error is different :)

Thank you!

sunshinejr commented 4 years ago

@Kalzem no problem at all, hope it helped with the issue :)