Question

i'm new to blocks, I have a class of requests with static methods to call me back on UIViewControllers with some blocks

this is the method implementation :

(putting a breakpoint on the block(something) DOES stop there, like it should)

+(void)requestSuggestedLocationsForText:(NSString*)text withBlock:(void (^)(NSArray*callBackArray))block
{
    if ([text isEqualToString:@""] || [text isEqualToString:@" "])
    {
        block(nil);
        return;
    }
    NSString * key = @"someActualKeyHere";

    ;


    NSString * finalText;
    NSArray *tagschemes = [NSArray arrayWithObjects:NSLinguisticTagSchemeLanguage, nil];
    NSLinguisticTagger *tagger = [[NSLinguisticTagger alloc] initWithTagSchemes:tagschemes options:0];
    [tagger setString:text];
    NSString *language = [tagger tagAtIndex:0 scheme:NSLinguisticTagSchemeLanguage tokenRange:NULL sentenceRange:NULL];
    if ([language isEqualToString:@"he"])
    {

        finalText = [text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    }
    else
    {
        finalText = [text stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    }

    NSString *urlString = [NSString stringWithFormat:
                           @"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=%@&types=geocode&sensor=true&key=%@",finalText,key];


    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    // 2
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (!responseObject && ![responseObject respondsToSelector:@selector(dataWithData:)])
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving "
                                                                message:@"ERROR"
                                                               delegate:nil
                                                      cancelButtonTitle:@"Ok"
                                                      otherButtonTitles:nil];
            [alertView show];
            return ;
        }
        NSData * responseData = [NSData dataWithData:responseObject];

        NSString *responseString = [NSString stringWithUTF8String:[responseData bytes]];
        NSError *err;
        if  ([responseString respondsToSelector:@selector(JSONObjectWithData:options:error:)])
        {
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&err];


            NSArray * predictions = [json valueForKey:@"predictions"];

            block(predictions);
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        // 4
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];

    // 5
    [operation start];

}

this is how I call it, notice the NSLog, i put a breakpoint on it and its never called which is exactly what I want to occur.

[Requests requestSuggestedLocationsForText:text withBlock:^(NSArray *callBackArray)
{
    NSLog(@"ROFL");
}];

for the record, I have tried the same method with a different signature (without the returning variable name like so :

+(void)requestSuggestedLocationsForText:(NSString*)text withBlock:(void (^)(NSArray*))block;

still didn't fire my breakpoint :(

Was it helpful?

Solution

I think that this:

if  ([responseString respondsToSelector:@selector(JSONObjectWithData:options:error:)])
{
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&err];
    NSArray * predictions = [json valueForKey:@"predictions"];
    block(predictions);
}

Never runs because as far as I know, NSString doesn't declare JSONObjectWithData. Your break point will never hit because it will never be called.

It seems like it could just be:

NSData * responseData = [NSData dataWithData:responseObject];
NSError *err;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err];

if (!err) {
    NSArray * predictions = [json valueForKey:@"predictions"];
    block(predictions);
}
else {
    block(nil);
}

The other way you convert it to a string, then back to data, why not just keep it as data?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top