【发布时间】:2014-01-11 10:49:20
【问题描述】:
我有一个应用程序,在该应用程序中我调用了一个 Web 服务来检索具有给定 ID 的 JSON 对象。 无论我在哪个类,获取对象的方法在系统上都是相同的,但成功块会有所不同(id est,处理部分)——例如使用 AFNetworking。
我正在寻找仅实现一次 getter 部分但能够自定义处理的正确方法。
下面这段代码是个好方法吗:
-(void)getObjectWithId:(NSString*)id_{
NSString *ns1 = [NSString stringWithFormat:@"%@%@%@",HOSTNAME,API_DETAIL,id_];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:ns1]];
AFJSONRequestOperation *operation =[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
//Here I want to do different processing task accordingly inheritance level // current class
DLog(@"Response : %@ \n Request : %@",response,request);
[self processObject:JSON];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
//Do failure stuff
}];
[operation start];
}
然后:
-(void)processObject:(id)JSON{
//Customize according to current class
}
因此,所有子类都将继承自 getObjectWithId 并拥有自己的 processObject 实现
我还应该考虑什么?是正确的方法吗?
【问题讨论】:
标签: ios objective-c json block afnetworking