swift - Check if NSUserDefaults key exists -


in application, saving key using code:

func savekey(){         var xminespillere = minespillere         var defaults = nsuserdefaults.standarduserdefaults()         defaults.setobject(xminespillere, forkey: "yourkey")     } 

but how can check if key exists? want code this:

if key("yourkey") exists {     println("key exists") } else {     println("does not exist") } 

how can in swift?

first of every time save nsuserdefaults need call synchronize() method writes modifications persistent domains disk , updates unmodified persistent domains on disk.

func savekey(){     var xminespillere = minespillere     var defaults = nsuserdefaults.standarduserdefaults()     defaults.setobject(xminespillere, forkey: "yourkey")     defaults.synchronize() } 

the synchronize method automatically invoked @ periodic intervals, use method if cannot wait automatic synchronization (for example, if application exit) or if want update user defaults on disk though have not made changes.

then can reach value in following way:

if let key = nsuserdefaults.standarduserdefaults().objectforkey("yourkey"){    // exist } else {    // not exist } 

i hope you.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -