zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    js、css等文件引入空白问题
    Vue 组件 data为什么是函数
    安装Node,创建vue项目,运行及打包
    JQuery移除事件
    百度地图定位
    移动端导航过多,点击导航左右滚动回弹
    移动端开发模板
    移动端左右滑动导航
    使用‘圣杯布局’、‘双飞翼布局’来解释自适应的三栏水平布局
    css实现三角箭头
  • 原文地址:https://www.cnblogs.com/sayimba/p/6097056.html
Copyright © 2011-2022 走看看