始终从iOS中的函数获得零值 [英] Always got nil value from the function in iOS

查看:88
本文介绍了始终从iOS中的函数获得零值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AFNetworking进行一些GET查询。但我的函数总是返回nil值。哪里错了?

I'm using AFNetworking for some GET queries. But my function always returns nil value. Where I was wrong?

+ (NSString *)getRequestFromUrl:(NSString *)requestUrl {
    NSString * completeRequestUrl = [NSString stringWithFormat:@"%@%@", BASE_URL, requestUrl];
    __block NSString * results;
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:completeRequestUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        results = [NSString stringWithFormat:@"%@", responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        results = [NSString stringWithFormat:@"Error"];
    }];
    NSLog(@"%@", results);
    return results;
}

Thx! Artem。

Thx! Artem.

推荐答案

您没有看到结果,因为您为成功传递的块失败异步运行;当您的 NSLog 被调用时,Web服务还没有返回。如果在成功和失败块中移动 NSLog ,您应该看到结果输出到控制台。

You're not seeing a result because the blocks that you pass in for success and failure run asynchronously; by the time your NSLog gets called, the web service won't have returned yet. If you move your NSLog inside of your success and failure blocks, you should see a result get output to your console.

由于这些调用的异步性,您将无法简单地从方法返回值。相反,您可能希望将自己的块作为参数,然后在获得结果时可以调用该参数。例如:

Because of the asynchronous nature of these calls, you won't be able to simply return the value from your method. Instead, you may want to take your own block as a parameter, which you can then call when you have a result. For example:

+ (void)getRequestFromUrl:(NSString *)requestUrl withCompletion:((void (^)(NSString *result))completion 
{
    NSString * completeRequestUrl = [NSString stringWithFormat:@"%@%@", BASE_URL, requestUrl];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:completeRequestUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *results = [NSString stringWithFormat:@"%@", responseObject];
        if (completion)
            completion(results);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSString *results = [NSString stringWithFormat:@"Error"];
        if (completion)
            completion(results);
    }];
}

然后你会这样调用你的方法:

You would then call your method like so:

[YourClass getRequestFromUrl:@"http://www.example.com" withCompletion:^(NSString *results){
    NSLog(@"Results: %@", results);
}

AFNetworking的示例项目有一个使用块参数返回值的示例您的网络服务电话: https://github.com/ AFNetworking / AFNetworking / blob / master / Example / Classes / Models / Post.m

AFNetworking's sample project has an example of using a block parameter to return a value from your web service calls: https://github.com/AFNetworking/AFNetworking/blob/master/Example/Classes/Models/Post.m

这篇关于始终从iOS中的函数获得零值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆