gpt4 book ai didi

ios - 关于 sendsynchronousRequest :returningResponse:error: is deprecated:first seprecated in ios 9- 的警告

转载 作者:行者123 更新时间:2023-11-29 10:23:37 34 4
gpt4 key购买 nike

它说 use [NSURLSession sharedsession] dataTaskwithRequest:request completionHandler:]

这里是我的代码:

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

我把上面的代码改成了这样:

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

我收到了 2 个警告:

  1. Null 传递给需要非 null 参数的被调用者

  2. 使用“NSURLSession”类型的表达式初始化“NSData”的不完整指针类型

帮帮我。请用有助于理解的代码向我解释。我是 ios 新手

我的实际代码:

   -(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];




}

最佳答案

您需要像这样使用该方法:

// 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];

请查看dataTaskWithRequest:completionHandler:获取更多详细信息。

关于ios - 关于 sendsynchronousRequest :returningResponse:error: is deprecated:first seprecated in ios 9- 的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33529452/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com