Warning about sendsynchronousRequest:returningResp

2019-08-10 08:08发布

问题:

And it says use [NSURLSession sharedsession] dataTaskwithRequest:request completionHandler:]

So here my code:

 NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

I changed above code to like this:

 NSData *returnData = [[NSURLSession  sharedSession] dataTaskWithRequest:request completionHandler:nil];

I got 2 warning:

  1. Null passed to a callee that requirs a non-null argument

  2. Incomplete pointer type initializing 'NSData' with a expression of type 'NSURLSession'

Help me out.Please do explain me with code that will helpfull to understand. I am new to ios

My actual code:

   -(void)getdata {

    NSString *userName = @“user@yahoo”;
    NSString *password = @“passr”;
    NSData *plainData = [password dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [plainData base64EncodedStringWithOptions:0];
    base64String=[self sha256HashFor: base64String];

    NSString *urlString = @"https://api.eaxmpleurl/files";

    NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"GET"];



    NSString *authStr = [NSString stringWithFormat:@"%@:%@", userName, base64String];
    NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];


    NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];

       [request setValue:authValue forHTTPHeaderField:@"Authorization"];










    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]
                                      dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
    {
        // Use the data here







        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


        NSError * error1;

        self->arrayPDFName = [[NSMutableArray alloc]init];
        NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];


        NSDictionary *dictOriginal = jsonResults[@"dark”];
    [titleArray addObject:[NSString stringWithFormat:@" Dark(%@)”, dictOriginal[@"count"]]];


    NSDictionary *dictOriginal2 = jsonResults[@"opey”];
    [titleArray addObject:[NSString stringWithFormat:@" Opey(%@)”, dictOriginal2[@"count"]]];



    NSDictionary *dictOriginal3 = jsonResults[@"pef”];
    [titleArray addObject:[NSString stringWithFormat:@" Pef(%@)”, dictOriginal3[@"count"]]];


    NSDictionary *dictOriginal4 = jsonResults[@"sdf”];
    [titleArray addObject:[NSString stringWithFormat:@" Sdf(%@)”, dictOriginal4[@"count"]]];


        NSArray *arrayFiles = [NSArray arrayWithObjects: dictOriginal, dictOriginal2, dictOriginal3, dictOriginal4, nil];


        NSLog(@"str: %@", titleArray);


        for (NSDictionary *dict in arrayFiles) {
            NSMutableArray *arr = [NSMutableArray array];

            NSArray *a = dict[@"files"];
            for(int i=0; i < a.count; i ++) {


                NSString *strName = [NSString stringWithFormat:@"%@",[[dict[@"files"] objectAtIndex:i] valueForKey:@"name"]];
                // NSLog(@"str: %@", strName);
                [arr addObject:strName];
            }
            [arrayPDFName addObject:arr];
        }











                NSString *errorDesc;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory1 = [paths objectAtIndex:0];
        NSString *plistPath = [documentsDirectory1 stringByAppendingPathComponent:@"SampleData.plist"];


        NSString *error2;


        data  = [ NSPropertyListSerialization dataWithPropertyList:jsonResults format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];


        if(data ) {
            if ([data  writeToFile:plistPath atomically:YES]) {
                NSLog(@"Data successfully saved.");
            }else {
                NSLog(@"Did not managed to save NSData.");
            }
        }
        else {
            NSLog(@"%@",errorDesc);
        }



        NSDictionary *stringsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];




#pragma unused (stringsDictionary)
#pragma unused (error1)

#pragma unused (str)








    }];

    // Starting the task
    [dataTask resume];




}

回答1:

You need to use that method like:

// Creating a data task
NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]
  dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
  // Use the data here
}];

// Starting the task
[dataTask resume];

Please check dataTaskWithRequest:completionHandler: for more detailed information.