標籤:efi atomic 作用 cell nsstring span address text code
主要作用:簡化VC代碼,便於請求資料中欄位的增、刪、查、找,以及後期代碼維護。
一、構建Model。
建立繼承於NSObject的PlaceOrderModel
#import <Foundation/Foundation.h>@interface PlaceOrderModel : NSObject//保證下面的欄位和請求的欄位相同即可,添加新欄位時可直接使用,新欄位對舊欄位沒有影響@property (nonatomic,strong) NSString * provinceName;@property (nonatomic,strong) NSString * cityName;@property (nonatomic,strong) NSString * countyName;@property (nonatomic,strong) NSString * unitName;@property (nonatomic,strong) NSString * address;@property (nonatomic,strong) NSString * contact;@property (nonatomic,strong) NSString * phone;+(instancetype)ModelWithDic:(NSMutableDictionary*)dic;@end
#import "PlaceOrderModel.h"@implementation PlaceOrderModel+(instancetype)ModelWithDic:(NSMutableDictionary *)dic{ PlaceOrderModel *model=[[PlaceOrderModel alloc]init]; [model setValuesForKeysWithDictionary:dic]; return model;}-(void)setValue:(id)value forUndefinedKey:(NSString *)key{ if ([key isEqualToString:@""]) { NSLog(@"資料不對"); } }
二、在VC中調用PlaceOrderModel
PlaceModel = [PlaceOrderModel ModelWithDic:_dataSouce[i]]; NSString * cityName = nil; if ([PlaceModel.provinceName isEqualToString:PlaceModel.cityName]) { cityName = [NSString stringWithFormat:@"%@%@",PlaceModel.provinceName,PlaceModel.countyName]; }else{ cityName = [NSString stringWithFormat:@"%@%@%@",PlaceModel.provinceName,PlaceModel.cityName,PlaceModel.countyName]; } cell.CityName.text = cityName; cell.AddressName.text = PlaceModel.address; NSString * people = [NSString stringWithFormat:@"%@ %@",PlaceModel.contact,PlaceModel.phone]; cell.PeopleName.text = people;
ios-model資料結構