I'm beginner in ios. This is a simple code example with 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);
}];
The responseObject would be printed to the console, but my question is : How to get responseObject and use it in an another class or methods?
EDIT 1:
I'm tried to use a method :
- (void)responseFromBlock:(id)response {
NSLog(@"responseObject from the block : %@",response);
}
And i'm use it inside the block :
[self responseFromBlock:responseObject];
But the answer is:
response object from the block : <7b226572 726f7222 3a7b2263 6f646522 3a313230 307d2c22 72657375 6c74223a 5b7b2263 6964223a 22313234 222c2263 6e616d65 223a2244 69766174 227d2c7b 22636964...
It might look like this:
@interface YourClassDealingWithAFNetworking
@property (nonatomic, retain) id object;
@end
And then inside the block:
self.object = responseObject;
Now you've got it and can use it wherever you need
just don't forget to release it in dealloc:
self.object = nil;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加