Save string to the NSUserDefaults?

180,434

Solution 1

NSString *valueToSave = @"someValue";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferenceName"];
[[NSUserDefaults standardUserDefaults] synchronize];

to get it back later

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
    stringForKey:@"preferenceName"];

Solution 2

more precisely

-(void)saveToUserDefaults:(NSString*)myString
{
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

    if (standardUserDefaults) {
        [standardUserDefaults setObject:myString forKey:@"timestamps"];
        [standardUserDefaults synchronize];
    }
}

Solution 3

Here's how to do the same with Swift;

var valueToSave = "someValue"
NSUserDefaults.standardUserDefaults().setObject(valueToSave, forKey: "preferenceName")

To get it back later;

if let savedValue = NSUserDefaults.standardUserDefaults().stringForKey("preferenceName") {
    // Do something with savedValue
}

In Swift 3.0

var valueToSave = "someValue"
UserDefaults.standard.set(valueToSave, forKey: "preferenceName")

if let savedValue = UserDefaults.standard.string(forKey: "preferenceName") {

}

Solution 4

Something like this:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];

Then to retrieve:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];

You should really check out Apple's NSUserDefaults Class Reference and also maybe this tutorial: iPhone Programming Tutorial – Saving/Retrieving Data Using NSUserDefaults

Solution 5

Do not forget this statement because otherwise it may not always work:

[standardUserDefaults synchronize];
Share:
180,434

Related videos on Youtube

Shishir.bobby
Author by

Shishir.bobby

From Java to Android to iOS App Developer to Project Manager(now).

Updated on April 19, 2022

Comments

  • Shishir.bobby
    Shishir.bobby about 2 years

    How to save a string into the NSUserDefaults?

  • jbrennan
    jbrennan almost 14 years
    There is no need to perform the nil check for standardUserDefaults object. It is perfectly fine to send messages to nil in Objective-C.
  • PostCodeism
    PostCodeism over 13 years
    But more importantly he included "synchronize" - NSUserDefaults is very erratic if you don't call this!
  • iOS.Lover
    iOS.Lover about 12 years
    Is there any way to save this string base on specific date ? thanks but your code works as same as mine , I create a custom calendar app which user can write their notes in it , for example in 3 Feb user writes something and this text should be saved in 3 Feb , and when user move to the another days he/she could write something else accruing to thad day date
  • Scott Roepnack
    Scott Roepnack over 11 years
    standardUserDefaults synchronize fixed some strange bugs for me, thanks!
  • eliocs
    eliocs over 10 years
    The reference talks about the synchronize method: "use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit)" - NSUserDefaults Class Reference
  • fs_tigre
    fs_tigre about 10 years
    For some reason the saving part didn't work properly until I called the synchronize method. NSString *valueToSave = @"someValue";[[NSUserDefaults standardUserDefaults]setObject:valueToSave forKey:@"preferenceName"]; [[NSUserDefaults standardUserDefaults]synchronize
  • Basil Bourque
    Basil Bourque almost 10 years
    @jbrennan While your comment is technically correct, this answer is helpful in case you are writing something important and want to be sure it was saved successfully. Furthermore, in that regard, it would be good to test (rather than ignore) the return value of synchronize. The doc says: YES if the data was saved successfully to disk, otherwise NO.
  • Juan Boero
    Juan Boero about 8 years
    [[NSUserDefaults standardUserDefaults] synchronize]; is getting deprecated
  • Akshansh Thakur
    Akshansh Thakur almost 8 years
    Thanks, since the original question was for swift, +1
  • Roy Falk
    Roy Falk about 7 years
    I'm having trouble with my cynicism detector after lunch. The original question was asked in 2010. I therefore upvoted this since Swift is the way to go today.
  • deniz
    deniz over 3 years
    As of iOS 12, calling synchronize is no longer needed: stackoverflow.com/a/57218546/12484