网络数据常用解决方案

1、查看JSON数据
2、服务器返回空数据

1、JSON数据

  • 当我们要看服务器返回的数据时,最好的方法就是转成plist文件。
  • 或者使用http://tool.oschina.net/codeformat/json工具
    // 1、拿到路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"top250" ofType:@"json"];
    
    // 2.创建tops
    self.tops = [[NSMutableArray alloc] init];
    
    // 解析json,kNilOptions效率很好
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    [dict writeToFile:@"/Users/mac2/Desktop/top.plist" atomically:YES];

2、如果服务器返回数据为nil或null如何解决

// 宏定义
#define NULL_TO_NIL(obj) ({ __typeof__ (obj) __obj = (obj); __obj == [NSNull null] ? nil : obj; })

// 字典转模型赋值的
cinema.lowPrice = NULL_TO_NIL([dict objectForKey:@"lowPrice"]);

// cell 中赋值判断
if (cinema.lowPrice == NULL) {
        self.priceLabel.text = @"暂停服务";
    } else {
        self.priceLabel.text = [NSString stringWithFormat:@"¥%@", cinema.lowPrice];
    }

你可能感兴趣的:(网络数据常用解决方案)