Using NSUserDefaults(sharedPreferences) in ios application

Samsul Hoque
9



Use NSUserDefaults: - note that this is for small bits of data, such as the current level like you mentioned. Don't abuse this and use it as a large database, because it is loaded into memory every time you open your app, whether you need something from it or not (other parts of your app will also use this).

Objective-C:

Reading:
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];

NSString *currentLevelKey = @"currentlevel";

if ([preferences objectForKey:currentLevelKey] == nil)
{
    //  Doesn't exist.
}
else
{
    //  Get current level
    const NSInteger currentLevel = [preferences integerForKey:currentLevelKey];
}
Writing:
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];

NSString *currentLevelKey = @"currentlevel";

const NSInteger currentLevel = ...;
[preferences setInteger:currentLevel forKey:currentLevelKey];

//  Save to disk
const BOOL didSave = [preferences synchronize];

if (!didSave)
{
    //  Couldn't save (I've never seen this happen in real world testing)
}
.

Swift:

Reading:
let preferences = NSUserDefaults.standardUserDefaults()

let currentLevelKey = "currentLevel"

if preferences.objectForKey(currentLevelKey) == nil {
    //  Doesn't exist
} else {
    let currentLevel = preferences.integerForKey(currentLevelKey) //Integer Value
let currentLevel = preferences.objectForKey(currentLevelKey) as! String            
    let currentLevel = preferences.boolForKey(currentLevelKey)//Boolean value
}
Writing:
let preferences = NSUserDefaults.standardUserDefaults()

let currentLevelKey = "currentLevel"

let currentLevel = ...
preferences.setInteger(currentLevel, forKey: currentLevelKey)
// store string value
preferences.setObject(saveValue, forKey: currentLevelKey)
//
 preferences.setBool(saveValue, forKey: currentLevelKey)
// Save to disk let didSave = preferences.synchronize() if !didSave { // Couldn't save (I've never seen this happen in real world testing) }

Refer

Post a Comment

9Comments
  1. Data Gardener finds and verifies information about specific company and its directors. It develops detailed profiles of target companies with accurate information.Let's follow our website:- https://datagardener.com

    ReplyDelete
  2. Our Bluetooth headphones offer gives a good experience that perfectly fits snugly over your ears.Please check: Bluetooth headphones wireless

    ReplyDelete
  3. If you are looking supply chain finance service in UK then connect with Whitehall Finance.
    Visit Here:- https://www.whitehallfinance.com/
    Call On:- (0203) 369 0098

    ReplyDelete
  4. Nice Blog, When i was read this blog i learnt new things & its truly have well stuff related to developing technology, Thank you for sharing this blog.
    iPhone app training course in bangalore
    Mobile app training institutes bangalore

    ReplyDelete
  5. If you want to become a professional IOS developer. you should learn IOS. By learning IOS you can able to create build views. You can handle user interaction via controls like buttons, switches, and sliders etc. We can also able to display data in a table view. You can create alert boxes, handle navigation and transition between views. You can also follow iOS App Development online Course

    ReplyDelete
Post a Comment