• 总结iOS9中的新的方法


    iOS平台在快速的发展,各种接口正在不断的更新。随着iOS9的发布,又有一批老方法不推荐使用了,你若调用这些方法,运行的结果是没有问题的,但是会出现警告“***is deprecated :first deprecated in iOS 9.0 - Use *******”.就像如图所示:

     

    在实际项目开发中,我们要秉承一个信念就是:要把每一个警告当做错误来处理,并解决每一个警告。你想想,你运行一个项目,就算运行成功了,但是出现几十个、甚至几百个黄黄的警告,心情是不是很糟糕呢?我将在这篇博客结合我的开发经验,罗列出iOS9中不推荐使用的方法,并换成苹果最新推荐的方式。本篇博客将会持续更新。

    1.【弹出提示对话框】

    在iOS9之前我们使用AlertView来弹出对话框,现在推荐使用AlertController

     

    2.【stringByAddingPercentEncodingWithAllowedCharacters替换stringByAddingPercentEscapesUsingEncoding】

    这个方法真的好长。。。我们使用这个方法来进行字符串编码方式的更改。最常用的地方就是进行Http网络请求的时候,发送的链接的参数中如果带有中文,那么首先就需要调用这个方法把编码方式改为utf8,因为服务器端一般都使用utf8编码。两者实现的功能一样。

     

    1
    2
    //以下方法已经不推荐使用;
      //  NSString *urlStr = [@http://v.juhe.cn/weather/index?format=2&cityname=北京&key=88e194ce72b455563c3bed01d5f967c5stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    1
    2
    //建议使用这个方法stringByAddingPercentEncodingWithAllowedCharacters,不推荐使用stringByAddingPercentEscapesUsingEncoding;
     NSString *urlStr2 = [@http://v.juhe.cn/weather/index?format=2&cityname=北京&key=88e194ce72b455563c3bed01d5f967c5 stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];



     

    3.【NSURLSession替换NSURLConnection】

    NSURLSession已经渐渐走上历史舞台了。最近使用[NSURLConnection sendAsynchronousRequest]时已经警告为不推荐使用了,那我们就换成NSURLSession中的dataTaskWithRequest方法吧。

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    //以下方法已经被禁用;
      NSOperationQueue *queue = [[NSOperationQueue alloc] init];
     
    [NSURLConnection
     sendAsynchronousRequest:urlRequest
     queue:queue
     completionHandler:^(NSURLResponse *response,
                         NSData *data,
                         NSError *error) {
        
       if ([data length] >0  &&
           error == nil){
         NSString *html = [[NSString alloc] initWithData:data
                                                encoding:NSUTF8StringEncoding];
         resault=[html copy];
          
         NSLog(@返回的服务器数据 = %@, html);
       }
       else if ([data length] == 0 &&
                error == nil){
         NSLog(@Nothing was downloaded.);
       }
       else if (error != nil){
         NSLog(@发生错误 = %@, error);
       }
        
     }];
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    //推荐使用这种请求方法;
    NSURLSession *session = [NSURLSession sharedSession];
     
    __block  NSString *result = @;
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       
      if (!error) {
        //没有错误,返回正确;
        result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@返回正确:%@,result);
         
      }else{
        //出现错误;
        NSLog(@错误信息:%@,error);
      }
       
    }];
     
     
    [dataTask resume];
  • 相关阅读:
    mysql之innodb_buffer_pool
    PBO项目的组织
    03 【PMP】组织结构类型的优缺点和适用范围包括哪些
    02 【PMP】项目管理系统、PMIS、工作授权系统、配置管理系统、变更管理
    01 【PMP】组织结构类型
    手工sql注入简单入门
    新建oracle用户
    linux测试环境搭建步骤
    1、python接口测试requests
    No module named pip 安装工具提示没有pip模块时,解决办法
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/5596071.html
Copyright © 2020-2023  润新知