3
\$\begingroup\$

I am using AFNetworking 1.4.3 to send and receive network messages in iOS. My application works slightly differently in DEBUG and RELEASE mode, so I need to use #ifdef clauses. How can I simplify the following fragments without making these macros:

  • #ifdef DEBUG
            if (failure) failure([NSError fromDict:responseObject]);
    #else
            if (failure) failure([NSError networkError]);
    #endif
    
  • #ifdef DEBUG
            if (failure) failure(error);
    #else
            if (failure) failure([NSError networkError]);
    #endif
    

Whole code:

@implementation ELHTTPClient

- (instancetype)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (self) {
        __weak ELHTTPClient *weakSelf = self;
        [self setParameterEncoding:AFJSONParameterEncoding];
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
        [self setDefaultHeader:@"Accept" value:@"application/json"];
        [self setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            [weakSelf.delegate httpClient:weakSelf connectionStateChanged:status];
        }];
    }
    return self;
}

- (void)myPostPath:(NSString *)path parameters:(NSDictionary *)parameters success:(void (^)(NSDictionary *))success failure:(ELErrorBlock)failure {
    [self postPath:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject isResponseValid]) {
            if (success) success(responseObject);
        }
        else {
#ifdef DEBUG
            if (failure) failure([NSError fromDict:responseObject]);
#else
            if (failure) failure([NSError networkError]);
#endif
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [self.delegate httpClient:self networkError:error];
//        NSLog(@"Network error. Operation: %@, \n Error: %@", operation, error);
#ifdef DEBUG
        if (failure) failure(error);
#else
        if (failure) failure([NSError networkError]);
#endif
    }];
}

- (void)myGetPath:(NSString *)path parameters:(NSDictionary *)parameters
          success:(void (^)(NSDictionary *dict))success
          failure:(ELErrorBlock)failure {
    [self getPath:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject isResponseValid]) {
            if (success) success(responseObject);
        }
        else {
#ifdef DEBUG
            if (failure) failure([NSError fromDict:responseObject]);
#else
            if (failure) failure([NSError networkError]);
#endif
//            NSError *error = [self.serializer errorFromDictionary:responseObject];
//            [self.delegate httpClient:self requestFailedError:error];
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
#ifdef DEBUG
        if (failure) failure(error);
#else
        if (failure) failure([NSError networkError]);
#endif
    }];
}

@end

My implementation of NSError category:

@implementation NSError (My)

+ (NSError *)networkError {
    NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
    [userInfo setValue:@"Some problems with connection to network. Please try again later." forKey:NSLocalizedDescriptionKey];//FIXME: add localized string.
    return [NSError errorWithDomain:@"Connection error" code:1024 userInfo:userInfo];
}

+ (NSError *)fromDict:(NSDictionary *)dict {
    NSString *errorDesc = dict[@"description"];
    errorDesc = errorDesc ? : [NSString stringWithFormat:@"Dictionary: \n%@", dict];
    NSMutableDictionary* details = [NSMutableDictionary dictionary];
    details[NSLocalizedDescriptionKey] = errorDesc;
    NSNumber *errorCodeNum = dict[@"errorCode"];
    return [NSError errorWithDomain:@"world" code:[errorCodeNum intValue] userInfo:details];
}
@end
\$\endgroup\$
2
  • \$\begingroup\$ Is +[NSError fromDict:] defined in AFNetworking or where does not come from? \$\endgroup\$
    – Martin R
    Oct 27, 2014 at 14:21
  • \$\begingroup\$ No, it's my own implementation. See edited content \$\endgroup\$
    – Szu
    Oct 27, 2014 at 14:23

1 Answer 1

2
\$\begingroup\$

I would put ifdef DEBUG inside the failure method, so it's handled in one place only.

(void) failure(id response, NSError error) {

#ifdef DEBUG

#else

#endif

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.