Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#ifdef statement to check for testing scheme

I have added a test target for my project, now I want to separate parts of the code that will not be executed on the app target. Let's say the app called TestMyApp and I want it to be something like:

-(void)addDevice:(Account*)account{
NSURL *url = [NSURL URLWithString:K_THINKERBELL_SERVER_URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];    


NSDictionary *params = @{@"device_id":account.deviceID,
                         @"token":account.deviceToken ? account.deviceToken : @"fuygciureygfiyuergfyurgfuyergfuyerguy",
                         @"type":account.deviceName,
                         @"os_type":@"ios",
                         @"os_version":account.os,
                         @"accounts":_accounts};


NSString *js = [jsonWriter stringWithObject:params];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"PUT" path:@"/device" parameters:nil];
NSData *requestData = [NSData dataWithBytes:[js UTF8String] length:[js length]];
[request setHTTPBody:requestData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request
    success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error = nil;
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
        if (error) {
            NSLog(@"%@",JSON);
        }
        NSLog(@"%@",JSON);
        if ([[JSON objectForKey:@"status"]isEqualToString:@"success"]) {

***#ifdef TestMyApp
   some code here***
  [self.testDelegate addDeviceJson:JSON type:@"addDevice"];
                                                            [self deviceAdded:[JSON objectForKey:@"uid"]];
                                                            [self.accountsDelegate deviceDidAdded];
                                                        }
                                                    }
                                                                        failure:^(AFHTTPRequestOperation *operation, NSError *error){
                                                                        }];
[httpClient enqueueHTTPRequestOperation:operation];
}
like image 828
or azran Avatar asked Feb 16 '23 06:02

or azran


2 Answers

Go to Target, Preprocessing section. In "Preprocessor macros" you can add defines for any scheme. For example:

TESTING=1

Then you can write

#ifdef TESTING
...
#endif

or

#if TESTING
...
#endif
like image 163
hybridcattt Avatar answered Feb 17 '23 20:02

hybridcattt


Create two pch file and add these to each of the target created. Now define some const string and based on that handle the code changes.

like image 21
Prasad_R Avatar answered Feb 17 '23 18:02

Prasad_R