Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • User default in swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.48k
    Comment on it

    Hi ,

    To use userDefault in swift we can use below code to

    1- Save value in userDefaults for a key

    2-  Fetch value for a key

    3- Remove value for a key

    4- Remove all value

    Here is the code -
     

    1. //Save a value in NSUserDefaults in swift
    2.  
    3. let userDefault = NSUserDefaults().standardUserDefaults()
    4. userDefault .setValue(accessToken, forKey: KEY_TO_SAVE_VALUE)
    5.  
    6. //remove a value from NSUserDefault in swift
    7.  
    8. let userDefault = NSUserDefaults.standardUserDefaults()
    9. userDefault.removeObjectForKey(KEY_TO_REMOVE_VALUE)
    10.  
    11. //remove all the values in NSUserDefaults in swift
    12.  
    13. let appdomain = NSBundle.mainBundle().bundleIdentifier
    14. let userPref = NSUserDefaults.standardUserDefaults()
    15. userPref.removePersistentDomainForName(appdomain!)
    16.  
    17. //print all the values from NSUserDefaults in swift
    18.  
    19. print("all the user default values here:\n(NSUserDefaults.standardUserDefaults().dictionaryRepresentation())")

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: