对plist构建数据模型,完成封装实现数据分离
来源:互联网 发布:淘宝指数查询 编辑:程序博客网 时间:2024/06/02 06:12
pList是我们常用的数据格式,首先来看我们的pList文件:是一个数组,里面存放字典,字典分为两项,name对应名字,icon对应图片名字,我们还有一组以此命名的图片。
对此,为了封装数据,我们建一个类,取名AppInfo.
AppInfo.h文件:
#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface AppInfo : NSObject@property (nonatomic,copy) NSString *name;@property (nonatomic,copy) NSString *icon;@property (nonatomic,strong,readonly) UIImage *image;/** 使用字典实例化 */+ (instancetype)initWithDictionary:(NSDictionary *)dict;/** 类方法快速实例化 */+ (instancetype)appInfoWithDictionary:(NSDictionary *)dict;+ (NSArray *)appList;@end
AppInfo.m文件:
#import "AppInfo.h"@implementation AppInfo@synthesize image = _image;- (UIImage *)image{ if(_image == nil ){ _image = [UIImage imageNamed:self.icon]; } return _image;}- (instancetype)initWithDictionary:(NSDictionary *)dict{ self = [super init]; if (self) { //KVC [self setValuesForKeysWithDictionary:dict]; } return self;}+ (instancetype)appInfoWithDictionary:(NSDictionary *)dict{ return [[self alloc]initWithDictionary:dict];}+(NSArray *)appList{ NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]]; NSMutableArray *arrayM = [NSMutableArray array]; for (NSDictionary *dict in array) { AppInfo *appInfo = [AppInfo appInfoWithDictionary:dict]; [arrayM addObject:appInfo]; } return arrayM;}@end
这样,在需要取数据的时候,只需调用 :
NSArray *array = [AppInfo appList];就能得到一个AppInfo数组,其中Image,name都已经封装到了AppInfo中,直接取值即可。
0 0
- 对plist构建数据模型,完成封装实现数据分离
- ios开发(plist文件数据加载) 使用数据模型的方法加载plist文件中的数据
- Bean对象封装,数据分离
- 从plist读取数据并封装
- 插件中对动态表单数据模型进行赋值实现动态表单数据绑定
- 超重点!使用ResultSetMetaDate完成数据查询与数据库实现的完全分离
- 对POST数据实现自动过滤和自动完成
- json对数据封装
- 在C中实现对struct内部数据的封装
- iOS对FMDB进行封装实现缓存数据
- runtime实现数据模型,自动绑定数据
- UITableView实现从plist获取数据
- 数据模型封装示例
- iOS开发中如何对数据与数据模型进行处理
- BeanUtils对数据的封装
- JavaDto对数据的封装
- JavaDto对数据的封装
- linux下用cjson实现对数据键值对的封装保存
- POJ2003 Hire and Fire
- 编写一个在1,2,…,9(顺序不能变)数字之间插入+或-或什么都不插入,使得计算结果总是100的程序,并输出所有的可能性。例如:1 + 2 + 34 – 5 + 67 – 8 + 9 = 100。
- 程序员必须注意的十大编程禁忌
- 数据库的一对一,一对多,多对多
- 整理js跳转到指定页面代码
- 对plist构建数据模型,完成封装实现数据分离
- Go语言爬取网站磁力链接
- mod_python
- DIV+CSS
- 易语言调用 vs2010 vc++ 写出的dll带参数 报堆栈错误的问题
- Java抽象类和接口
- 分辨率大全
- 用Java实现计算“1+3+5+7+8+9+...+99”
- 同一个窗口中不同的按钮控件使用同一个storyboard的方法(WPF)