Ios 如何从块内部获取对象并在另一个类或方法中使用它?
我是ios的初学者。这是一个使用AFNetworking的简单代码示例Ios 如何从块内部获取对象并在另一个类或方法中使用它?,ios,afnetworking,objective-c-blocks,Ios,Afnetworking,Objective C Blocks,我是ios的初学者。这是一个使用AFNetworking的简单代码示例 AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://yourServerAddress.com/"]]; [httpClient setParameterEncoding:AFFormURLParameterEncoding]; NSMutableURLRequest *request =
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...
来自块的响应对象:它可能如下所示:
@interface YourClassDealingWithAFNetworking
@property (nonatomic, retain) id object;
@end
然后在街区内:
self.object = responseObject;
现在你有了它,可以在任何需要的地方使用它
只是别忘了在dealloc中发布它:
self.object = nil;
在开始编写iOS应用程序之前,您应该学习面向对象编程。“在Objective C2.0中编程”是一本初学者的好书。这个问题很简单!