Ios 在AFNetworking 2.0中有没有一种简化代码的方法?
现在,在我的代码中,我有很多以下代码块的副本:Ios 在AFNetworking 2.0中有没有一种简化代码的方法?,ios,objective-c,cocoa,afnetworking,afnetworking-2,Ios,Objective C,Cocoa,Afnetworking,Afnetworking 2,现在,在我的代码中,我有很多以下代码块的副本: NSString *imageID = [postURL.path substringFromIndex:1]; NSString *APILink = [NSString stringWithFormat:@"https://api.imgur.com/3/image/%@", imageID]; AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationM
NSString *imageID = [postURL.path substringFromIndex:1];
NSString *APILink = [NSString stringWithFormat:@"https://api.imgur.com/3/image/%@", imageID];
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager.requestSerializer setValue:@"Client-ID --myID--" forHTTPHeaderField:@"Authorization"];
[operationManager GET:APILink
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
....
有没有办法将其简化为更可重用的代码块?我记得在AFNetworking 1.0中将
AFHTTPClient子类化以实现类似的功能,但我不清楚如何在AFNetworking 2.0中实现它。您可以将代码放入AFHTTPRequestOperationManager
类别中的方法中:
如果我分析的代码正确:
myservicesapiaaddition.h
@interface AFHTTPRequestOperationManager(MyServicesAPIAddition)
+ (void)resourceWithURL:(NSURL*)URL clientID:(NSString *)clientID completionHandler:(void (^)(AFHTTPRequestOperation *operation, id responseObject))handler;
@end
myservicesapiaaddition.m
@implementation AFHTTPRequestOperationManager(MyServicesAPIAddition)
+ (void)resourceWithURL:(NSURL*)URL clientID:(NSString *)clientID completionHandler:(void (^)(AFHTTPRequestOperation *operation, id responseObject))handler
{
NSString *imageID = [URL.path substringFromIndex:1];
NSString *APILink = [NSString stringWithFormat:@"https://api.imgur.com/3/image/%@", imageID];
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager.requestSerializer setValue:[NSString stringWithFormat:@"Client-ID %@", clientID] forHTTPHeaderField:@"Authorization"];
[operationManager GET:APILink
parameters:nil
success:handler];
}
@end
然后:
[AFHTTPRequestOperationManager resourceWithURL:postURL clientID:… completionHandler:
^(AFHTTPRequestOperation *operation, id responseObject)
{
…
}
人们希望在方法名称中添加后缀,以防止选择器冲突。我没有