uni*_*der 5 iphone api objective-c asihttprequest
我目前有一个视图控制器,它实现ASIHTTP来处理API调用.
我的视图控制器触发2个单独的调用.我需要能够区分-requestFinished(ASIHTTPRequest*)请求方法中的2个调用,因此我可以相应地解析每个调用...
有没有这样做?
使用userInfo字段!这就是它的用途!
ASIHTTPRequest(或ASIFormDataRequest)对象具有一个名为.userInfo的属性,该属性可以将NSDictionary包含在您想要的任何内容中.所以我总是去:
- (void) viewDidLoad { // or wherever
ASIHTTPRequest *req = [ASIHTTPRequest requestWithUrl:theUrl];
req.delegate = self;
req.userInfo = [NSDictionary dictionaryWithObject:@"initialRequest" forKey:@"type"];
[req startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
if ([[request.userInfo valueForKey:@"type"] isEqualToString:@"initialRequest"]) {
// I know it's my "initialRequest" .req and not some other one!
// In here I might parse my JSON that the server replied with,
// assemble image URLs, and request them, with a userInfo
// field containing a dictionary with @"image" for the @"type", for instance.
}
}
Run Code Online (Sandbox Code Playgroud)
@"type"在此视图控制器中为每个不同的ASIHTTPRequest 设置键上对象的不同值,您现在可以区分它们-requestFinished:并适当地处理它们中的每一个.
如果您真的很喜欢,可以随身携带在请求完成时有用的任何其他数据.例如,如果您正在加载延迟图像,则可以向自己传递要填充的UIImageView的句柄,然后在-requestFinished加载图像数据后执行此操作!
| 归档时间: |
|
| 查看次数: |
2366 次 |
| 最近记录: |