Skip to content

kvo not work #297

@pmlican

Description

@pmlican

here my example,when click button change usename observe not work, is anything i miss?

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        Defaults.observe(\.username, options: [.initial, .old, .new]) { update in
            print(update)
        }

    }

    @IBAction func click(_ sender: Any) {
        let num = Int.random(in: 0..<10)
        
        let username = Defaults[\.username]
        Defaults[\.username] = " abc \(num)"
    }
}

extension DefaultsKeys {
    var username: DefaultsKey<String> { .init("username", defaultValue: "default") }
 
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions