源码:swiftDemo ocDemo
使用swift给模型赋值: 赋值方法三种: 一,属性逐一赋值 二,在构造函数中赋值 三,重写setValuesForKeysWithDictionary赋值 对于构造函数(或是重写setValuesForKeysWithDictionary方法)赋值,操作如下: 声明一个静态字符串数组,由属性所对应的键(key)构成 使用for循环遍历静态字符串数组, 在for循环遍历里,使用key取出dictionary的value,并赋值给Model setValue(value: AnyObject?, forKey: String) for循环可以写在构造函数中, 也可以使用重写setValuesForKeysWithDictionary的方法实现 代码:
class Model: NSObject { var name = "" var job = "" var image = "" static let properties = ["name","job","image"] override init() { super.init() } init(dict : [String : AnyObject?]) { super.init() for key in Model.properties { if dict[key] != nil { setValue(dict[key]!, forKey: key) } } } override func setValuesForKeysWithDictionary(keyedValues: [String : AnyObject]) { for key in Model.properties { if keyedValues[key] != nil { setValue(keyedValues[key]!, forKey: key) } } } } 调用: /*一 模型对象,属性逐一赋值 */ let model = Model.init(); model.name = "Lily" model.job = "teacher" model.image = "icon" /* 二 使用构造函数赋值 */ let m = Model.init(dict: ["name":"lily","job":"Student","image":"icon"]) /* 三 重写setValuesForKeysWithDictionary赋值 */ let model = Model.init(); model.setValuesForKeysWithDictionary(["name":"lily","job":"Student","image":"icon"]) swift中的set和get方法 声明: var _model : Model? var model : Model?{ get{ return _model } set{ icon.image = UIImage(named: newValue!.image) name.text = newValue!.name job.text = newValue!.job _model = newValue } } 使用: cell.model = modelArray![indexPath.row] 使用swift用模型给view赋值 以TableViewCell为例, 在cell中使用model赋值两种方法: 一,直接声明实现一个函数传入Model即可,例如 func cellForModel(model : Model) -> () { 在该函数实现中赋值,在cell实例化后,使用cell的对象调用这个函数, cell.cellForModel(modelArray![indexPath.row]) 二,使用set和get方法赋值, 首先声明两个Model类型的变量,便于代码阅读,这两个变量名称最好相似,一下划线区分它俩的不同 在其中一个变量后面实现set和get方法,在set方法中赋值, 在cell实例化后,使用cell的对象调用声明的属性赋值即可,例如 cell.model = modelArray![indexPath.row] 代码示例: class TableViewCell: UITableViewCell { @IBOutlet weak var icon: UIImageView! @IBOutlet weak var name: UILabel! @IBOutlet weak var job: UILabel! var _model : Model? var model : Model?{ get{ return _model } set{ icon.image = UIImage(named: newValue!.image) name.text = newValue!.name job.text = newValue!.job _model = newValue } } func cellForModel(model : Model) -> () { icon.image = UIImage(named: model.image) name.text = model.name job.text = model.job } } 调用代码示例: /* 一 使用函数赋值 */ cell.cellForModel(modelArray![indexPath.row]) /* 二 使用set方法赋值 */ cell.model = modelArray![indexPath.row]Ios中使用OC时, 模型赋值 oc数据模型 赋值方法常用的两种, 一 在.h文件的接口中声明属性(.m中接口声明的属性类外部不可以访问),使用时对属性逐一赋值 二 在.h文件的接口中声明属性(.m中接口声明的属性类外部不可以访问),使用构造函数赋值,需要注意,构造函数声明需写在.h的接口中,这样类外部可以调用该函数 构造函数有两种,类函数和类函数,它们区别在于调用时,类函数使用类名直接调用,函数需要构造类对象,才能调用 在构造函数中,使用setValuesForKeysWithDictionary赋值 实例代码: 在Model.h中
#import <Foundation/Foundation.h> @interface Model : NSObject @property ( nonatomic , copy ) NSString * name; @property ( nonatomic , copy ) NSString * job; @property ( nonatomic , copy ) NSString * image; - (instancetype)initWithDict : (NSDictionary *) dict ; + (instancetype)model : (NSDictionary *) dict ; @end 在Model.m中 #import "Model.h" @implementation Model - (instancetype)initWithDict:(NSDictionary *)dict { if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self; } + (instancetype)model:(NSDictionary *)dict { /* Model *m = [[super alloc]init]; if (m) { [m setValuesForKeysWithDictionary:dict]; } return m; */ return [[super alloc]initWithDict:dict]; } @end 调用代码实例: /* 构造函数赋值 一 */ Model * model0 = [Model model:@{@"name":@"lily",@"job":@"Teacher",@"image":@"icon"}]; /* 构造函数赋值 二 */ Model * model = [[Model alloc]initWithDict:@{@"name":@"lily",@"job":@"Teacher",@"image":@"icon"}]; /* 调用setValuesForKeysWithDictionary */ Model * model = [[Model alloc]init]; [model setValuesForKeysWithDictionary:@{@"name":@"lily",@"job":@"Teacher",@"image":@"icon"}]; 使用模型对象给View赋值 以TableViewCell为例 数据模型给cell赋值: 常用方法两种: 一 使用set方法赋值,在.h文件中声明一个类型为Model的变量,在.m文件夹重写set方法,在set方法内部赋值 二 声明传参变量为Model的函数,调用赋值,直接传入Model的参数,在函数实现内部赋值 示例代码: 在TableViewCell.h中 #import <UIKit/UIKit.h> @class Model ; @interface TableViewCell : UITableViewCell @property ( nonatomic , strong ) Model * model ; - (void) cellForModel : (Model *) model ; @end 在TableViewCell.m中 #import "TableViewCell.h" #import "Model.h" @interface TableViewCell () @property (weak, nonatomic) IBOutlet UIImageView *icon; @property (weak, nonatomic) IBOutlet UILabel *name; @property (weak, nonatomic) IBOutlet UILabel *job; @end @implementation TableViewCell - (void)setModel:(Model *)model { _model = model; _icon.image = [UIImage imageNamed:model.image]; _name.text = model.name; _job.text = model.job ; } - (void)cellForModel:(Model *)model { _icon.image = [UIImage imageNamed:model.image]; _name.text = model.name; _job.text = model.job ; } @end 调用代码示例: /* set方法赋值 */ cell.model = _modelArray[indexPath.row] ; /* 调用函数赋值 */ [cell cellForModel:_modelArray[indexPath.row]];