ios-model数据结构

Posted sayimba

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios-model数据结构相关的知识,希望对你有一定的参考价值。

主要作用:简化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数据结构的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段5——HTML元素结构

VSCode自定义代码片段5——HTML元素结构

超级有用的9个PHP代码片段

python 用于数据探索的Python代码片段(例如,在数据科学项目中)

将代码片段插入数据库并在 textarea 中以相同方式显示

分享几个实用的代码片段(第二弹)