在另一端没有被调用

时间:2014-04-08 23:21:21

标签: ios objective-c syntax objective-c-blocks

我是块新手,我有一类请求使用静态方法在UIViewControllers上用一些块调用我

这是方法实现:

(在块上设置一个断点(某些东西)停在那里,就像它应该的那样)

+(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];

}

这就是我所说的,注意NSLog,我在它上面放了一个断点,它从未调用过 这正是我想要发生的事情。

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

对于记录,我尝试了使用不同签名的相同方法(没有返回变量名称,如下所示:

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

仍然没有触发我的断点:(

1 个答案:

答案 0 :(得分:1)

我认为:

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);
}

从不运行,因为据我所知,NSString没有声明JSONObjectWithData。你的断点永远不会被击中,因为它永远不会被召唤。

似乎可能只是:

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);
}

另一种方法是将它转换为字符串,然后再转换回数据,为什么不将它保存为数据呢?

相关问题