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