0

我是ios的初学者。这是一个使用 AFNetworking 的简单代码示例。

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://yourServerAddress.com/"]];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
                                                        path:@"http://yourServerAddress.com/example?name=foo"
                                                  parameters:nil];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request
                                        success:^(AFHTTPRequestOperation *operation, id responseObject) {


                                            NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
                                        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                            //-- This block gets invoked when the operation fails
                                            NSLog(@"Error: %@", error.localizedDescription);
                                        }];

responseObject 将被打印到控制台,但我的问题是:如何获取 responseObject 并在另一个类或方法中使用它?

编辑1:

我试图使用一种方法:

- (void)responseFromBlock:(id)response {

NSLog(@"responseObject from the block : %@",response);

}

我在块内使用它:

 [self responseFromBlock:responseObject];

但答案是:

response object from the block : <7b226572 726f7222 3a7b2263 6f646522 3a313230 307d2c22 72657375 6c74223a 5b7b2263 6964223a 22313234 222c2263 6e616d65 223a2244 69766174 227d2c7b 22636964... 
4

1 回答 1

0

它可能看起来像这样:

@interface YourClassDealingWithAFNetworking

@property (nonatomic, retain) id object;

@end

然后在块内:

self.object = responseObject;

现在你已经拥有它并且可以在任何你需要的地方使用它

只是不要忘记在 dealloc 中释放它:

self.object = nil;
于 2013-07-30T10:12:05.970 回答