Thursday, September 1, 2016

Using NSUserDefaults(sharedPreferences) in ios application





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

11 comments :

  1. Great site for these post and i am seeing the most of contents have useful for my Carrier.Thanks to such a useful information.Any information are commands like to share him.

    AWS Training in Chennai

    ReplyDelete
  2. I just keep visiting this blog from past two years. The posts here are unique and useful than other blog posts. Such a great info provided in this post. Keep providing useful post in this blog .I appreciate author for this valuable post. Dubai Cooking Schools, International Centre for Culinary Arts

    ReplyDelete
  3. I am very happy when read this blog post because blog post written in good manner and write on good topic. Thanks for sharing valuable information.
    Bangalore Web Designing Company, Web Development Company Bangalore

    ReplyDelete
  4. Great Posts… I love to read your posts because your writing style is too good, it is very helpful for all of us and I never get tedious while reading your article because, they are becomes a more and more interesting from the starting lines until the end. I really appreciate your post. Well Done.
    Engineering Colleges, ECE Engineering Colleges in Chennai

    ReplyDelete
  5. A very well-written post. I read and liked the post and have also bookmarked you. All the best for future endeavors. Getting some solution regarding.
    UI UX Design Companies in Bangalore, Web Application Development in Bangalore

    ReplyDelete

 

Copyright @ 2014 Tech Tutorial .