ios model字典ios
#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
atom
@implementation AdObject + (instancetype)carGroupWithDict:(NSDictionary *)dict { AdObject *cg = [[self alloc] init]; [cg setValuesForKeysWithDictionary:dict]; return cg; } @end 引用 AdObject *cg = [AdObject initWithDictionary:dict]; //alloc也給封裝了,
獲取本地文件spa
NSArray *tmp = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars_simple" ofType:@"plist"]];
NSArray使用小結code
//類方法數組建立 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];