Showing posts with label Language Selection. Show all posts
Showing posts with label Language Selection. Show all posts

Saturday, November 19, 2016

Permanent Storage with Default System in iPhone

Sometimes we required to store our choices with the Device
like:
 Language Selection ,  Units of Measurement etc

With iPhone you can do all these by using class 
 NSUserDefaults , it provides a programmatic interface for interacting with the defaults system. 
The defaults system allows an application to customize its behavior to match a user’s preferences. 

You can store six types 
1. NSData
2. NSString
3. NSNumber ( Int ,Float , Double , Bool )
4. NSDate
5. NSArray
6. NSDictionary

For Writing the way will be as 
let data = NSUserDefaults.standardUserDefaults(); // Default
data.setObject("Value for Key ", forKey: "KeyName")
     // Set Value for the Key 


Like i am going to add my First Name  as Ravi with key name
data.setObject("Ravi", forKey: "name")

For Reading the way will be as 

let data = NSUserDefaults.standardUserDefaults();
let getData =  data.objectForKey("fname")!  

Now getData can be used for any purpose as per your App Requirements.

The another Methods are 
  • func setBool(Bool Value , "KeyName")
  • func setInteger(Int Value ,  "KeyName")
  • func setFloat(Float Value ,  "KeyName")
  • func setDouble(Double Value ,  "KeyName")
  • func setObject(value: AnyObject?,  "KeyName")
  • func setURL(url: NSURL?, f "KeyName")
keyName should be String.