[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 ie 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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.