• NSJSONSerialization的简单用法


    NSJSONSerialization

    苹果官方给出的解析方式是性能最优越的,虽然用起来稍显复杂。

    首先我们在上面已经有了我希望得到的信息的网站的API给我们的URL,在OC中,我要加载一个NSURL对象,来向网站提交一个Request。到这里需要特别注意了,iOS9的时代已经来临,我们先前在旧版本中使用的某些类或者方法都已经被苹果官方弃用了。刚刚我们向网站提交了一个Request,在以往,我们是通过NSURLConnection中的sendSynchronousRequest方法来接受网站返回的Response的,但是在iOS9中,它已经不再使用了。从官方文档中,我们追根溯源,找到了它的替代品——NSURLSession类。这个类是iOS7中新的网络接口,苹果力推之,并且现在用它完全替代了NSURLConnection。关于它的具体用法,还是蛮简单的,直接上代码(ViewController.m文件):

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property (weak, nonatomic) IBOutlet UITextView *textView;
    
    @property (nonatomic, strong) NSMutableDictionary *dic;
    
    @property (nonatomic, strong) NSString *text;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    - (IBAction)didClickNSJsonButton:(id)sender {
        //GCD异步实现
        dispatch_queue_t q1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(q1, ^{
            NSURL *url = [NSURL URLWithString:@"https://api.douban.com/v2/movie/subject/25881786"];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            //使用NSURLSession获取网络返回的Json并处理
            NSURLSession *session = [NSURLSession sharedSession];
            NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                self.dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                NSString *title = [self.dic objectForKey:@"original_title"];
                NSMutableArray *genresArray = [self.dic objectForKey:@"genres"];
                NSString *genres = [NSString stringWithFormat:@"%@/%@",[genresArray objectAtIndex:0],[genresArray objectAtIndex:1]];
                NSString *summary = [self.dic objectForKey:@"summary"];
                self.text = [NSString stringWithFormat:@"电影名称:%@
    体裁:%@
    剧情介绍:%@",title,genres,summary];
                //更新UI操作需要在主线程
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.textView.text = self.text;
                });
            }];
            
            [task resume];
        });
    }
    

    有时候我们在使用第三方网络访问包的时候,导入的时候会出现一堆错误,这可能是因为它不支持ARC,这时候不用慌,我们会发现大部分是ARC的问题,解决方法也挺简单,我们进入项目的Target,找到Build Phases里面的Compile Sources,接着找我们的问题源头*.m文件,双击更改它的Compiler Flags标签为“-fno-objc-arc”,再次编译,就好啦~

    盒岸兼职

  • 相关阅读:
    [BZOJ2287]【POJ Challenge】消失之物(DP)
    [BZOJ1596] [Usaco2008 Jan]电话网络(树形DP || 贪心)
    [BZOJ3339] Rmq Problem(线段树)
    [BZOJ3378] [Usaco2004 Open]MooFest 狂欢节(树状数组)
    [BZOJ1595] [Usaco2008 Jan]人工湖(单调栈)
    [BZOJ1594] [Usaco2008 Jan]猜数游戏(二分 + 并查集)
    [POJ2778]DNA Sequence(AC自动机 + DP + 矩阵优化)
    [TyvjP1519] 博彩游戏(AC自动机 + DP)
    【模板】AC自动机
    [poj3744] Scout YYF I【概率dp 数学期望】
  • 原文地址:https://www.cnblogs.com/foxting/p/5568520.html
Copyright © 2020-2023  润新知