-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.

6
  • Any error? What does NSLog say? Commented Apr 24, 2015 at 11:34
  • for this line key=[response valueForKey:@"key"]; Variable is not assignable (missing __block type specifier) Commented Apr 24, 2015 at 11:36
  • Are you sure you get that JSON back ? What is the content of response or responseObject? Commented Apr 24, 2015 at 11:38
  • yes. with out that line log print data Commented Apr 24, 2015 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? Commented Apr 24, 2015 at 11:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.