NSUserDefaults. Retrieving incorrect values - ios

I am saving few dictionaries in NSUserDefaults this way:
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"event_states"] == nil)
{
NSDictionary *dict0 = [NSDictionary dictionaryWithObjectsAndKeys:
#"923381DC-3DCB-46CA-A6CF-C58A7AF33B89",#"guid",
#"В планах",#"name", //this string will returned corrupted
[NSNumber numberWithInt:12632256],#"color",
[NSNumber numberWithInt:0],#"isclosed",
[NSNumber numberWithInt:0],#"isfinish",
nil];
// and few more dictionaries same way
....
[[NSUserDefaults standardUserDefaults] setObject:#[dict0,dict1,dict2,dict3] forKey:#"event_states"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
NSArray *event_states = [[NSUserDefaults standardUserDefaults] objectForKey:#"event_states"];
When I am retrieving this data I am logging it and have a strange behavior. In some cases I am retrieving a correct value as this:
name = "\U0412 \U043f\U043b\U0430\U043d\U0430\U0445"
but in some cases as this:
name = "\U0412\U044b\U043f\U043e\U043b\U043d\U0435\U043d(\U043e)
and this last part in parenthesis is an extra symbol and I can't understand why it appears and what is dependence on when it appears and when it's not. What can be wrong with this?

I tried to convert your unicode to readable string and paste it in google translate
\U0412 \U043f\U043b\U0430\U043d\U0430\U0445 => В планах => The plans
\U0412\U044b\U043f\U043e\U043b\U043d\U0435\U043d(\U043e) => Выполнен(о) => Complete ?
So I think this happened because your code not the API.

Related

Objective-C : Can't set NSMutableDictionary from NSUserDefaults

I'm trying to retrieve an NSDictionary that was saved to NSUserDefaults. When I log out what I'm trying to retrieve, it displays correctly:
NSLog(#"%#", [[[NSUserDefaults standardUserDefaults] dictionaryForKey:#"userdetails"] mutableCopy]);
However, when I try to assign to a variable, it returns as nil.
NSMutableDictionary *test123 = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:#"userdetails"] mutableCopy];
Any idea why?
Check with my working code,
Save like this,
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:yourMutableDictionary];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:#"currentUser"];
[[NSUserDefaults standardUserDefaults]synchronize];
Get dictionary ,
NSData *dictionaryData = [[NSUserDefaults standardUserDefaults] objectForKey:#"currentUser"];
NSDictionary *tempDict = [NSKeyedUnarchiver unarchiveObjectWithData:dictionaryData];
Maybe you can try it use the following code:
[[NSUserDefaults standardUserDefaults] setObject:(NSDictionary *)yourDictionary forKey:yourkey];

How to store JSON response in string and then store that string locally ios objective c

[enter image description here][1]I get three values from JSON. How do I store those three values in NSString and then store those strings locally i.e on the device?
I tried NSUserDefaults but I'm unable to do it. I've attached the code snippet.
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:responseObject
options:0 // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! JSONData)
{
NSLog(#"Got an error: %#", error);
} else {
//_branchId = [responseObject valueForKey:#"branchId"];
_branchName = [responseObject valueForKey:#"branchName"];
_branchUri = [responseObject valueForKey:#"branchUri"];
[[NSUserDefaults standardUserDefaults] setObject:_branchId forKey:[responseObject valueForKey:_branchId]];
[[NSUserDefaults standardUserDefaults] setObject:_branchName forKey:#"branchName"];
[[NSUserDefaults standardUserDefaults] setObject:_branchUriStore forKey:#"_branchUri"]; // Here the setobject value remains nil only
[[NSUserDefaults standardUserDefaults] synchronize];
PS: I want to use that stored value in my commonutility also. How to do that as well?
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"(__fetchBranchUri)/%#",url]]];
In the else block replace the contents with
[[NSUserDefaults standardUserDefaults] setObject:[responseObject valueForKey:#"branchId"] forKey:#"branchId"];
[[NSUserDefaults standardUserDefaults] setObject:[responseObject valueForKey:#"branchName"] forKey:#"branchName"];
[[NSUserDefaults standardUserDefaults] setObject:[responseObject valueForKey:#"branchUri"] forKey:#"_branchUri"];
[[NSUserDefaults standardUserDefaults] synchronize];
Edit: Explanation
In your code above the object _branchUriStore is not defined, and may be part of your problem (a simple typo?). Below, I have updated and condensed your code to remove another problem with your usage of branchId and so that you might recognize from the style that much of what you were trying to do is fine.
Also note that #rmaddie has provided additional refinements below in the comments.
Okay here is what you should do.
1- convert your JSONData no NSData
2-Save your NSData in the NSUserDefaults/
Hope this helps!

Reading String With NSUserDefaults not working

I'm using iOS9 SDK, and I'm trying to read a string using :
NSString *savedID = [[NSUserDefaults standardUserDefaults] stringForKey:#"ID"];
I saved it using :
[[NSUserDefaults standardUserDefaults] setObject:[myPeripheral.identifier UUIDString] forKey:#"ID"];
[[NSUserDefaults standardUserDefaults] synchronize];
The problem is that the property savedID has a nil value.
When I execute the ligne bellow in the consol, I get the value I saved :
po [[NSUserDefaults standardUserDefaults] stringForKey:#"ID"];
PS : The value I saved is : 73BAE46D-3A74-E7F4-05F7-EFCB6AACC20C
I also tried to use objectForKey, but I still got the same result.
Any Idea?
I was debugging on a device, I tried to execute the App without debug and it worked :p
Thank you every one.

ios saving and retrieving NSNumber from NSUserdefaults

I've searched but I can't find a good explanation as to why I'm still not getting the right value from my saved Integer.
This is how I saved my event Id.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:eventSelected.eventId forKey:#"currentEvent"];
This is how I'm trying to retrieve it.
NSLog(#"user dfault %#", [NSNumber numberWithUnsignedInt:[[[NSUserDefaults standardUserDefaults] objectForKey:#"currentEvent"] unsignedIntegerValue]]);
I'm gettin gthis.
user dfault 123581200
Also, to note, event if I did intValue instead of unsignedIntegerValue , it would still give me a random id number. was wondering what I'm doing wrong.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// Convert your value to IntegerValue and Save it
[prefs setInteger:[eventSelected.eventId integerValue] forKey:#"currentEvent"];
// Dont forget to synchronize UserDefaults
[prefs synchronize];
// To Access this value
NSLog(#"%d",[prefs integerForKey:#"currentEvent"]);
Instead of this code NSLog(#"user dfault %#", [NSNumber numberWithUnsignedInt:[[[NSUserDefaults standardUserDefaults] objectForKey:#"currentEvent"] unsignedIntegerValue]]);
Use this
NSLog(#"user dfault %#", [[NSUserDefaults standardUserDefaults] integerForKey:#"currentEvent"]);
NSNumber give a pointer value, not integer value.
To retrieve the integer you should use below function:
[[NSUserDefaults standardUserDefaults] integerForKey:#"currentEvent"];
when you ask for objectForKey, it box it first and then return the object. Posting intValue will convert the address of that object into integer. Hence each time you will receive different number.
NSNumber *number;
store:
[[NSUserDefaults standardUserDefaults] setValue:number forKey:#"Number"];
retrieving:
number = [NSNumber numberWithInteger:[[NSUserDefaults standardUserDefaults] integerForKey:#"Number"]];

NSUserDefaults registerDefaults with NSInteger

I am using NSUserDefaults to store some integer settings for my app, but I want to register default values for these settings, using code like this:
NSDictionary *appDefaults = [[NSDictionary alloc] initWithObjectsAndKeys:(NSInteger)0, #"PMChordColour", (NSInteger)1, #"PMTextColour", (NSInteger)0, #"PMBackgroundColour", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
The values are read in my app using
NSInteger setColour = [[NSUserDefaults standardUserDefaults] integerForKey:#"PMChordColour"];
The problem with this is that NSDictionary does not allow scalar values to be stored.
Is there any way of registering defaults that allows integers to be stored? I realise that I could use NSNumber but it seems like unnecessary overhead.
No, you have to use NSNumber. It's necessary overhead.
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:13], #"id", nil];
int integer = [[dict objectForKey:#"id"] intValue];

Resources