ios model字典
#import <Foundation/Foundation.h> @interface AdObject : NSObject /// 广告ID @property (strong,nonatomic) NSString *adID; /// 广告图片的地址 @property (strong,nonatomic) NSString *imagePath; /// 广告标题 @property (strong,nonatomic) NSString *adTitle; /*! * @brief 自定义初始化方法 * @param dict 存放 HPAdObject 内容的字典 * @return 当前类的一个实体 */ - (id)initWithDictionary:(NSDictionary*)dict; @end
实现
@implementation AdObject - (id)initWithDictionary:(NSDictionary*)dict { if (self = [super init]) { _adID = dict[@"id"]; _imagePath = dict[@"img"]; _adTitle = dict[@"title"]; } return self; } @end 引用 AdObject *obj = [[AdObject alloc]initWithDictionary:dictItem];
kvc
@implementation AdObject + (instancetype)carGroupWithDict:(NSDictionary *)dict { AdObject *cg = [[self alloc] init]; [cg setValuesForKeysWithDictionary:dict]; return cg; } @end 引用 AdObject *cg = [AdObject initWithDictionary:dict]; //alloc也给封装了,
获取本地文件
NSArray *tmp = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_simple" ofType:@"plist"]];
NSArray使用小结
//类方法数组创建 NSArray *array1 = [NSArray arrayWithObject:@"obj"]; NSArray *array2 = [NSArray arrayWithObjects:@"obj1", @"obj2", @"obj3", nil]; NSArray *array3 = [NSArray arrayWithArray:array2]; //实例方法创建数组 NSArray *array4 = [[NSArray alloc] initWithObjects:@"AAA", @"bbb", nil]; //数组个数 NSLog(@"array3 count :%d", [array3 count]); //访问元素 NSLog(@"obj at index :%@", [array2 objectAtIndex:2]); //是否包含指定对象 NSLog(@"isContains :%d", [array5 containsObject:@"obj2"]); //查找某个对象所在索引 NSLog(@"indexOfObject :%d",[array5 indexOfObject:@"obj3"]); //最后一个元素 NSLog(@"lastObejct :%@", [array5 lastObject]); //遍历数组 for (id element in array5) { NSLog(@"element :%@", element); }
NSMutableArray;
NSMutableArray *MutableArray = [NSMutableArray alloc] init]; [mutableArray addObject:@"000"]; //插入元素 [mutableArray insertObject:@"ccc" atIndex:0]; //移除指定元素 [mutableArray removeObject:@"ccc"]; //移除指定下标元素 [mutableArray removeObjectAtIndex:0]; //移除最后一个元素 [mutableArray removeLastObject]; //添加数组 [mutableArray addObjectsFromArray:array5]; //移除指定数组中的内容 [mutableArray removeObjectsInArray:array2]; //指定索引替换对象 [mutableArray replaceObjectAtIndex:0 withObject:@"==="]; //删除全部对象 [mutableArray removeAllObjects];
NSDictionary
//创建多个字典 NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"key1", @"value2", @"key2", @"value3", @"key3", @"value4", @"key4", nil]; //根据key获取value NSLog(@"key3 value :%@", [dic3 objectForKey:@"key3"]); //获取字典数量 NSLog(@"dic count :%d", dic3.count); //所有的键集合 NSArray *keys = [dic3 allKeys]; //所有值集合 NSArray *values = [dic3 allValues];
NSMutableDictionary;
NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] init]; //添加现有的字典数据 [mutableDic addEntriesFromDictionary:dic3]; //添加新的键值对象 [mutableDic setValue:@"set1" forKey:@"setKey1"]; //以新的字典数据覆盖旧的字典数据 [mutableDic setDictionary:dic2]; //根据key删除value [mutableDic removeObjectForKey:@"key1"]; //快速遍历 for(id key in mutableDic) { NSLog(@"key :%@ value :%@", key, [mutableDic objectForKey:key]); } //枚举遍历 NSEnumerator *enumerator = [mutableDic keyEnumerator]; id key = [enumerator nextObject]; while (key) { NSLog(@"enumerator :%@", [mutableDic objectForKey:key]); key = [enumerator nextObject]; } //根据key数组删除元素 [mutableDic removeObjectsForKeys:keys]; //删除所有元素 [mutableDic removeAllObjects];