-2

I am trying to extract data(just a string) from request and set it to the NSString. I tried it in many way but it is not working. If anyone can point out my mistake, it will be very helpful for me.

json data

{
    "status": 1,
    "key": "1e39248f4a5e05153dc376a"
}

My code

NSString *key;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSDictionary *params = @ {@"app_token" :APP_TOKEN};

    [manager POST:GET_USER_KEY_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary* response = (NSDictionary*) responseObject;
        key=[response valueForKey:@"key"];
        [[NSUserDefaults standardUserDefaults]setValue:(key) forKey:USER_KEY];
        NSLog(@"NEW KEY Request: %@", key);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"NEW KEY Request error: %@", error);
    }];

Just want to assign response "key" data and store it on the NSString *key;

Thank you in advance.

Abdul Rehman Yawar Khan
  • 1,088
  • 3
  • 17
  • 40
gamal
  • 1,587
  • 2
  • 21
  • 43
  • Any error? What does NSLog say? – Miknash Apr 24 '15 at 11:34
  • for this line key=[response valueForKey:@"key"]; Variable is not assignable (missing __block type specifier) – gamal Apr 24 '15 at 11:36
  • Are you sure you get that JSON back ? What is the content of response or responseObject? – Miknash Apr 24 '15 at 11:38
  • yes. with out that line log print data – gamal Apr 24 '15 at 11:41
  • can you try objectForKey instead of valueForKey? and I am not sure what do you mean by 'without that line log print data'. When you log response you get that JSON from above? – Miknash Apr 24 '15 at 11:44
  • You have declared the variable `key` outside of the block. You need to add `__block` infront of `NSString *key;` – Fogh Apr 24 '15 at 11:59

1 Answers1

3

You have declared the variable key outside of the block. You need to add __block infront of NSString *key;

To assign a variable outside a block you have to remember the __block specifier.

Related question: Assign a variable inside a Block to a variable outside a Block

Community
  • 1
  • 1
Fogh
  • 1,275
  • 1
  • 21
  • 29