-2

[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]]];

2 Answers2

0

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.

digitalR
  • 11
  • 6
  • Use `objectForKey:`, not `valueForKey:` unless you have a specific need for KVC. And you don't need to call `synchronize`. – rmaddy May 27 '16 at 17:04
  • And how is this different from the code in the question except for combining some lines together? – rmaddy May 27 '16 at 17:05
  • There are errors in the apparent logic. Note the line: – digitalR May 27 '16 at 18:09
  • @rmaddy, There are errors in the apparent logic. Note the line: [[NSUserDefaults standardUserDefaults] setObject:_branchUriStore forKey:@"_branchUri"]; What is _branchUriStore? Of, course 'synchronize' in not required for each update of NSUserdefaults. But, such depends on the overall app logic, to which we are not privy. I just fixed the code that I saw. Perhaps a little too quickly so that I missed the 'value for key'. I do not think that I should be down voted. I will be more verbose next time. I thought this sight was leaner than that. – digitalR May 27 '16 at 18:18
  • You should put the explanation in your answer. Code-only answers don't really help. A good answer explains what was wrong in the OP and a good answer explains how their answer solves the problem. – rmaddy May 27 '16 at 18:24
  • Thank you it worked. And for all who have been asking that my string names are not defined, they are but in .h file. – Shishir Aggarwal May 28 '16 at 10:36
  • Glad it helped. Please "accept" the answer. I am new on this system and probably need to accumulate "points" to be more effective for others. – digitalR May 28 '16 at 19:50
-1

Okay here is what you should do.

1- convert your JSONData no NSData

2-Save your NSData in the NSUserDefaults/

Hope this helps!

Mohamad Bachir Sidani
  • 2,077
  • 1
  • 11
  • 17