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];
Related videos on Youtube
Author by
Shishir.bobby
From Java to Android to iOS App Developer to Project Manager(now).
Updated on April 19, 2022Comments
-
Shishir.bobby about 2 years
How to save a string into the
NSUserDefaults
?-
Jayprakash Dubey over 9 years
-
Ε Г И І И О about 9 yearsIf you are looking for Swift, then I have added an answer.
-
-
jbrennan almost 14 yearsThere is no need to perform the nil check for
standardUserDefaults
object. It is perfectly fine to send messages to nil in Objective-C. -
PostCodeism over 13 yearsBut more importantly he included "synchronize" - NSUserDefaults is very erratic if you don't call this!
-
iOS.Lover about 12 yearsIs 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 over 11 yearsstandardUserDefaults synchronize fixed some strange bugs for me, thanks!
-
eliocs over 10 yearsThe 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 about 10 yearsFor 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 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 about 8 years[[NSUserDefaults standardUserDefaults] synchronize]; is getting deprecated
-
Akshansh Thakur almost 8 yearsThanks, since the original question was for swift, +1
-
Roy Falk about 7 yearsI'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 over 3 yearsAs of iOS 12, calling
synchronize
is no longer needed: stackoverflow.com/a/57218546/12484