Save and Get data from NSUserDefaults in Swift.

You are use the NSUserDefault to store the data types value and use the value in single apps. The value store as long as app installed. This tutorial provide, How to store data type Like Bool, Float , Double, Int , String, NSUrl value store using NSUserDefault. You can also store Array, Dictionary and NSData value store in NSUserDefault.

When you set the value in NSUserDefault, then it automatically loaded the value at anytime and any of the Class.
The idea to store the data in NSUserDefault is good, But to store more value in NSUserDefault take too much time to load the apps, so make slow of Apps.  

PART 1 : Store in NSUserDefault

Create new instance of NSUserDefault in AppDelegate like this :
let kUserDefault = NSUserDefaults.standardUserDefaults()
Store the FullName dataType is String , and Key is  fullName.
 kUserDefault.setObject("Kirit Modi", forKey: "fullName")
 kUserDefault.synchronize()
Store Array with Key is nameArray.
 kUserDefault.setObject(["KIRIT" , "MODI" , "FIRST" , "LAST"], forKey: "nameArray")
 kUserDefault.synchronize()
Store Dictionary with Key is name.
 kUserDefault.setObject(["kirit" : "firstName" , "Modi" : "lastName"], forKey: "name")
 kUserDefault.synchronize()
Store Bool with Key is isCheck.
 kUserDefault.setBool(true, forKey: "isCheck")
 kUserDefault.synchronize()
Store Double with Key is average.
kUserDefault.setDouble(19.20, forKey: "average")
kUserDefault.synchronize()
Store Int with Key is age.

kUserDefault.setInteger(20, forKey: "age")
kUserDefault.synchronize()
Store Float with Key is floatValue.

kUserDefault.setFloat(12.20, forKey: "floatValue")
kUserDefault.synchronize()
Store NSUrl with Key is myUrl.

kUserDefault.setURL(NSURL(string: "www.google.com"), forKey: "myUrl")
kUserDefault.synchronize()
PART 2 : Get From NSUserDefault

Now You can get the all stored value at any of the Class, Here we will get the store value in ViewController Class. Now see how can get.

First of all, You create new instance of NSUserDefault in ViewController.
let kUserDefault = NSUserDefaults.standardUserDefaults()
Get fullName with Key fullName :
 kUserDefault.stringForKey("fullName")
 kUserDefault.objectForKey("fullName")
 kUserDefault.valueForKey("fullName")
StringForKey  : Swift String value with double quote Like "Kirit Modi" .
ObjectForKey : NSString value Like Kirit Modi .
ValueForKey  : NSString value Like Kirit Modi .

Get Array with Key nameArray :

kUserDefault.arrayForKey("nameArray")!
kUserDefault.objectForKey("nameArray")!
kUserDefault.valueForKey("nameArray")
arrayForKey : Swift Array Like : [KIRIT, MODI, FIRST, LAST].
ObjectForKey : NSArray Like : 
(
    KIRIT,
    MODI,
    FIRST,
    LAST

)

ValueForKey : NSArray Like : 
(
    KIRIT,
    MODI,
    FIRST,
    LAST


)



Get Dictionary with Key name :

kUserDefault.dictionaryForKey("name")!
kUserDefault.objectForKey("name")!
kUserDefault.valueForKey("name")
dictionaryForKey : Swift Dictionary Like : 

["Modi": lastName, "kirit": firstName]
ObjectForKey : NSDictionary Like : 
{
    Modi = lastName;
    kirit = firstName;
}
ValueForKey : NSDictionary Like : 
{
    Modi = lastName;
    kirit = firstName;
}


Get Bool with Key isCheck :

kUserDefault.boolForKey("isCheck")
kUserDefault.objectForKey("isCheck")!
kUserDefault.valueForKey("isCheck")
boolForKey     : Swift Bool Value Like :  true
ObjectForKey : Boolean value :  1 or 0
ValueForKey  :  Boolean value :  1 or 0

Get Double with Key average :

kUserDefault.doubleForKey("average")
kUserDefault.objectForKey("average")!
kUserDefault.valueForKey("average")
Get Int with Key age :

kUserDefault.integerForKey("age")
kUserDefault.objectForKey("age")!
kUserDefault.valueForKey("age")
Get NAUrl with Key myUrl :
kUserDefault.URLForKey("myUrl")
kUserDefault.objectForKey("myUrl")!
kUserDefault.valueForKey("myUrl")
URLForKey     :  Swift URL : www.google.com
ObjectForKey  :  URL Data 

62706c69 73743030 d4010203 04050616 17582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0a4 07080f10 55246e75 6c6cd309 0a0b0c0d 0e574e53 2e626173 65562463 6c617373 5b4e532e 72656c61 74697665 80008003 80025e77 77772e67 6f6f676c 652e636f 6dd21112 13145a24 636c6173 736e616d 65582463 6c617373 6573554e 5355524c a2131558 4e534f62 6a656374 5f100f4e 534b6579 65644172 63686976 6572d118 1954726f 6f748001 08111a23 2d32373c 42495158 6466686a 797e8992 989ba4b6 b9be0000 00000000 01010000 00000000 001a0000 00000000 00000000 00000000 00c0

ValueForKey   :  URL Data 
62706c69 73743030 d4010203 04050616 17582476 65727369 6f6e5824 6f626a65 63747359 24617263 68697665 72542474 6f701200 0186a0a4 07080f10 55246e75 6c6cd309 0a0b0c0d 0e574e53 2e626173 65562463 6c617373 5b4e532e 72656c61 74697665 80008003 80025e77 77772e67 6f6f676c 652e636f 6dd21112 13145a24 636c6173 736e616d 65582463 6c617373 6573554e 5355524c a2131558 4e534f62 6a656374 5f100f4e 534b6579 65644172 63686976 6572d118 1954726f 6f748001 08111a23 2d32373c 42495158 6466686a 797e8992 989ba4b6 b9be0000 00000000 01010000 00000000 001a0000 00000000 00000000 00000000 00c0


Thanks.

Save and Get data from NSUserDefaults in Swift. Save and Get data from NSUserDefaults in Swift. Reviewed by Kirit Modi on 05:34:00 Rating: 5

No comments:

Powered by Blogger.