gpt4 book ai didi

ios - 阻止另一端不被调用

转载 作者:行者123 更新时间:2023-11-29 03:04:24 25 4
gpt4 key购买 nike

我是 block 的新手,我有一类带有静态方法的请求,可以用一些 block 在 UIViewControllers 上回调我

这是方法实现:

(在 block 上放置一个断点(某物)确实停在那里,就像它应该的那样)

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

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

最佳答案

我认为:

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

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

关于ios - 阻止另一端不被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22950059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com