iOS Model Dictionary
#import <Foundation/Foundation.h> @interface adobject:nsobject///advertising id@property (strong,nonatomic) NSString * adid;///AD Image Address @property (strong,nonatomic) nsstring *imagepath;///ad title @property (strong,nonatomic) NSString * adtitle;/*! * @brief Custom Initialization method * @param dict the dictionary holding hpadobject content * @return an entity */-(ID) initwithdictionary for the current class: (nsdictionary*) dict; @end
Realize
@implementation adobject-(ID) initwithdictionary: (nsdictionary*) dict{if (self = [super init]) {_adid = Dict [@ "id"]; _imagepath = dict[@ "img"]; _adtitle = dict[@ "title"]; } return self;} @end reference Adobject *obj = [[Adobject Alloc]initwithdictionary:dictitem];
Kvc
@implementation adobject+ (Instancetype) cargroupwithdict: (nsdictionary *) dict{adobject *CG = [[Self alloc] init]; [CG SETVALUESFORKEYSWITHDICTIONARY:DICT]; return CG;} @end reference Adobject *CG = [Adobject initwithdictionary:dict]; The alloc is also encapsulated,
Get local file
Nsarray *tmp = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "cars_simple" ofType:@ "plist" ]];
Nsarray Usage Summary
The class method array is created nsarray *array1 = [nsarray arraywithobject:@ "obj"]; nsarray *array2 = [nsarray arraywithobjects:@ "Obj1", @ "Obj2", @ "Obj3", nil] ; nsarray *array3 = [nsarray arraywitharray:array2]; //instance method creates an array nsarray *array4 = [[nsarray alloc] initwithobjects:@ "AAA", @ "BBB", nil]; //Array number nslog (@ "array3 count :%d", [array3 count]); //Access Element nslog (@ "obj at index :%@ ", [array2 objectatindex:2]); //contains the specified object nslog (@" IsContains :%d ", [array5 containsobject:@" Obj2 "]); //finds an object's index nslog (@ "IndexOfObject :%d ", [Array5 indexofobject:@ "Obj3"]); //the last element nslog (@ "lastobejct :%@", [array5 lastobject]); //Traversal array for (ID&NBSP;ELEMENT&NBSP;IN&NBSP;ARRAY5) { nslog (@ "element :%@", element); }
Nsmutablearray;
nsmutablearray *mutablearray = [nsmutablearray alloc] init]; [mutablearray addobject:@ "]; //" Insert element [mutablearray insertobject:@ "CCC" atindex:0]; //Remove the specified element [mutablearray removeobject:@ "CCC"]; //removes the specified subscript element [mutablearray removeobjectatindex:0 ]; //removing the last element [mutablearray removelastobject]; //adding arrays [mutableArray addObjectsFromArray:array5]; //removes the contents of the specified array [mutableArray removeObjectsInArray:array2]; //specifying an index replacement object [mutableArray replaceobjectatindex:0 withobject:@ "= = ="]; //Delete all Objects [mutableArray removeAllObjects];
Nsdictionary
Create multiple dictionaries NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys: @ "value1", @ "Key1", @ "value2", @ "Key2", @ "Value3", @ "Key3", @ "Value4", @ "Key4", nil]; //gets Value nslog (@ "key3 based on key value :%@ ", [dic3 objectforkey:@" Key3 "]); //Get Dictionary number nslog (@ "dic count :%d", dic3.count); //all the key sets &NBSP;&NBSP;NSARRAY&NBSP;*KEYS&NBSP;=&NBSP;[DIC3 allkeys]; //All Values Collection nsarray *values = [dic3 allvalues];
Nsmutabledictionary;
nsmutabledictionary *mutabledic = [[nsmutabledictionary alloc] init];//to add existing dictionary data [mutabledic addentriesfromdictionary:dic3]; //Adding a new key-value object [mutableDic setvalue:@ "Set1" forkey:@ "SetKey1"]; //overwrite old dictionary data with new dictionary data [mutableDic setDictionary:dic2]; //Delete value [mutabledic removeobjectforkey:@ "Key1" according to Key; //Fast Traverse for (Id key in mutabledic) { nslog (@ "Key :%@ value :%@ ", key, [mutabledic objectforkey:key]); } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//Enumeration Traversal nsenumerator *enumerator = [ Mutabledic keyenumerator]; id key = [enumerator nextobject]; while (Key) { nslog (@ "enumerator :%@", [mutabledic objectforkey:key]); key = [enumerator nextObject]; } //deletes an element based on the key array [mutabledic removeobjectsforkeys : keys]; //Delete all elements [mutableDic Removeallobjects];
iOS model dictionary Nsarray nsdictionary Summary