NSDictionary 字典 KVC 的使用

2021-07-03 19:46:39 字數 4048 閱讀 2957

字典分為不可變字典(nsdictionary)和可變字典(nsmutabledictionary)

/* 不可變字典 */

1.建立字典 

nsdictionary *dic = [nsdictionary dictionarywithobjectsandkeys:

@"male",@"***",

@"20", @"age",

@"tom", @"name",

@"run", @"hobby", nil];

2.獲取所有的key值和value值

[dic allkeys];

[dic allvalues];

3.根據key值來獲取value值

[dic valueforkey:@"name"];
[dic objectforkey:@"name"];
// 寫法2 不可以寫 nil, 寫法1可以

4.遍歷字典 

nsarray *arr = [dic allkeys];

for (int i = 0; i < [arr count]; i++)

for (nsstring *key in dic)

//寫法2雖然寫的是遍歷字典,實際上還是遍歷陣列[dic allkeys];

/* 可變字典 */

1. 建立可變字典

nsmutabledictionary *mdic = [nsmutabledictionary dictionarywithobjectsandkeys:

@"male",@"***",

@"20", @"age",nil];

2.增加鍵值對

[mdic setvalue:@"tom" forkey:@"name"];

[mdic setobject:@"running" forkey:@"hobby"];

3.修改鍵值對

[mdic setvalue:@"18" forkey:@"age"];

//跟2.增加鍵值對方法是一樣的, 這個key值要是有就覆蓋掉原來的value, 如果沒有這個key值就相當於新新增一對鍵值對

4.刪除鍵值對

[mdic removeobjectforkey:@"age"];

[mdic setvalue:nil forkey:@"age"];

/*

model 類*/

oc中 繼承於 nsobject 的這些類統稱  model 類 (用於訪問字串)

model類更像可變字典

例題1 把下列格式的輸出用字典-陣列巢狀的形式寫出, 並轉成model類的形式 (

;header = (

); }

) [1]字典-陣列巢狀

nsmutablearray *bigarray = [nsmutablearray array];

nsmutabledictionary *bigdic = [nsmutabledictionary dictionary];

[bigarray addobject:bigdic];

nsmutabledictionary *bodydic = [nsmutabledictionary dictionary];

[bodydic setobject:@"錯誤資訊" forkey:@"message"];

[bodydic setobject:@"1" forkey:@"errorcode"];

[bigdic setobject:bodydic forkey:@"body"];

nsmutablearray *headerarray = [nsmutablearray array];

[bigdic setobject:headerarray forkey:@"header"];

nsmutabledictionary *little = [nsmutabledictionary dictionary];

[headerarray addobject:little];

[little setobject:@"10" forkey:@"page"];

[little setobject:@"婚紗" forkey:@"title"];

[little setobject:@"fwefasd" forkey:@"content"];

nsmutablearray *images = [nsmutablearray array];

[images addobject:@""];

[images addobject:@""];

[images addobject:@""];

[little setobject:images forkey:@"image"];

[2] model類的形式

//建立model類要從結構裡往外建立

1.建立headermodel

@inte***ce headermodel : nsobject

2.建立bodymodel

@inte***ce bodymodel : nsobject

3.建立bigmodel

@class bodymodel;

@inte***ce bigmodel : nsobject

4.建立了乙個工具類tools將功能進行封裝

.h

@class bigmodel;

@inte***ce tools : nsobject

+ (bigmodel *)getbigmodel;

.m

#import "tools.h"

#import "bigmodel.h"

#import "bodymodel.h"

#import "headermodel.h"

@implementation tools

+ (bigmodel *)getbigmodel

+ (nsstring *)getvaluefrom:(nsmutablearray *)array key:(nsstring *)content

main.m

nsmutablearray *bigarray = [nsmutablearray array];

bigmodel *b1 = [tools getbigmodel];

[bigarray addobject:b1];

bigmodel *b2 = [tools getbigmodel];

[bigarray addobject:b2];

/*kvc

*/key - value - coding

1. 給屬性賦值:

[dic setvalue:<#(id)#> forkey:<#(nsstring *)#>];

2. 最大的用處: 字典和model 的轉換

3.通過字典給model 賦值:

[model setvaluesforkeyswithdictionary:<#(nsdictionary *)#>];

必須在model類裡重寫:

-(void)setvalue:(id)value forkey:(nsstring *)key

空著就可以, 

如果沒有定義這個屬性, 就什麼都不用寫

NSDictionary 字典 集合NSSet

字典存在的價值 0.字典類是用於儲存具有對映關係 key value對 的資料,字典一旦建立,鍵值對就不可更改,不可新增,不可刪除.僅能讀取key 或者 value 1.大容器,用來儲存多個資料,2.用來儲存的資料具有 對應的關係.使用key 來標識 value 3.對於字典中的一對鍵值對 key ...

字典NSDictionary 使用注意

void viewdidload self dic dic self dicdata nsdictionary alloc init void touchesbegan nsset touches withevent uievent event if a nslog e nslog a,b,c,d ...

OC中字典NSDictionary用法

字典就是 鍵值對 建立字典的兩種方式 1.nsdictionary dic nsdictionary alloc initwithobjectsandkeys one 1 two 2 three 3 nil 2.nsdictionary dic1 字典中得所有 key value 都是無順序的。乙個...