質問

I have an existing app on the App Store that's suddenly developed a few issues related to the iOS7.1 update. This is one I can't get my head around though, it looks as though it should (and it used to) work. I'm trying to check whether the "Success" key of a JSON string is equal to '1'.

AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:req success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
    if ([[JSON objectForKey:@"Success"] isEqualToValue:[NSNumber numberWithInt:1]]) {
        // succeeded
        NSLog(@"Reached success while submitting");

    } else {
        NSLog(@"JSON: %@", [JSON objectForKey:@"Success"]);
        // Success was not 0
    }
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    // parse/download error
}];

The program always reaches the else statement even if the Success key is 1. Here's the debug print of the JSON object:

Printing description of JSON:
{
    EntryId = 5235;
    EntryLink = "https://cpdme.wufoo.com/api/v3/forms/z7x4a9/entries.json?Filter1=EntryId+Is_equal_to+5235";
    Success = 1;
}
役に立ちましたか?

解決

Consider changing your code to:

NSNumber *statusValue = [JSON objectForKey:@"Success"];

if ([statusValue boolValue]) {
    ...

as this has a much broader scope for correctly identifying truth. It will also quietly deal with the value being a string instance.

I guess your current code suffers from a class type mismatch during the comparison. Log the class and contents of [JSON objectForKey:@"Success"] and [NSNumber numberWithInt:1] to verify the results.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top