【移動應(yīng)用開發(fā)技術(shù)】iOS開發(fā)中KVC、KVO簡介_第1頁
【移動應(yīng)用開發(fā)技術(shù)】iOS開發(fā)中KVC、KVO簡介_第2頁
【移動應(yīng)用開發(fā)技術(shù)】iOS開發(fā)中KVC、KVO簡介_第3頁
【移動應(yīng)用開發(fā)技術(shù)】iOS開發(fā)中KVC、KVO簡介_第4頁
免費預覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】iOS開發(fā)中KVC、KVO簡介

在iOS開發(fā)中,KVC和KVO是經(jīng)常被用到的??梢允褂肒VC對對象的屬性賦值和取得對象的屬性值,可以使用KVO監(jiān)聽對象屬性值的變化。簡單介紹一下KVC和KVO。一:鍵值編碼(KVC)KVC,全稱KeyValueCoding(鍵值編碼),是OC語言的一個特性,使用KVC,可以對對象的屬性進行動態(tài)讀寫。KVC的操作方法由NSKeyValueCoding協(xié)議提供,而NSObject已經(jīng)實現(xiàn)了這個協(xié)議,因此OC中的幾乎所有對象都可以使用KVC操作。常用的KVC操作方法有:(1)設(shè)置屬性值setValue:valueforKey:key(用于簡單路徑,也就是直接屬性)setValue:valueforKeyPath:key(用于復雜路徑,也就是間接屬性)(2)獲取屬性值valueForKey:key

(用于獲取簡單路徑的屬性值)valueForKeyPath:key(用于獲取復雜路徑的屬性值)通過代碼來看KVC的具體使用:兩個類分別是Dog類和Person類Dog.h1

#import

<Foundation/Foundation.h>2

3

@interface

Dog

:

NSObject4

5

@property

(nonatomic,

copy)

NSString

*name;6

7

@endDog.m1

#import

"Dog.h"2

3

@implementation

Dog4

5

@endPerson.h

1

#import

<Foundation/Foundation.h>

2

3

@class

Dog;

4

5

@interface

Person

:

NSObject

6

7

@property

(nonatomic,

copy)

NSString

*name;

8

@property

(nonatomic,

copy)

NSString

*sex;

9

10

@property

(nonatomic,

strong)

Dog

*dog;11

12

@endPerson.m1

#import

"Person.h"2

3

@implementation

Person4

5

@endKVC的使用:

1

Person

*person

=

[[Person

alloc]

init];

2

//kvc

設(shè)置值

3

[person

setValue:@"Jim"

forKey:@"name"];

4

[person

setValue:@"boy"

forKey:@"sex"];

5

6

//kvc

獲得值

7

NSLog(@"name

=

%@

sex

=

%@",[person

valueForKey:@"name"],[person

valueForKey:@"sex"]);

8

9

Dog

*dog

=

[[Dog

alloc]

init];10

person.dog

=

dog;11

//kvc

設(shè)置復雜路徑值12

[person

setValue:@"Teddy"

forKeyPath:@""];13

//kvc

獲得復雜路徑值14

NSLog(@"dog

name

=

%@",[person

valueForKeyPath:@""]);可以看到,KVC使用對應(yīng)的函數(shù)即可設(shè)置值、獲得值。那么,KVC是如何查找一個屬性進行讀取呢?KVC遵循下面的規(guī)則,假設(shè)要對name屬性進行讀?。海?)設(shè)置屬性:優(yōu)先考慮setName方法;如果沒有,則搜索成員變量_name;如果沒找到,則搜索成員變量name;如果還沒有找到,則調(diào)用

setValue:forUndefineKey:

方法。(2)讀取屬性:優(yōu)先考慮getName方法;如果沒有,則搜索成員變量_name;如果沒找到,則搜索成員變量name;如果還沒有找到,則調(diào)用

valueForUndefineKey:

方法。二:KVC中setValuesForKeysWithDictionary:的使用KVC中有一個非常重要的方法:setValuesForKeysWithDictionary:dict,該方法可以將一個字典映射到一個對象,省去了給對象一一賦值的步驟。使用setValuesForKeysWithDictionary:dict的一個例子:student.h

1

#import

<Foundation/Foundation.h>

2

3

@interface

Student

:

NSObject

4

5

/**

6

*

學號

7

*/

8

@property

(nonatomic,

copy)

NSString

*num;

9

/**10

*

姓名11

*/12

@property

(nonatomic,

copy)

NSString

*name;13

/**14

*

身高15

*/16

@property

(nonatomic,

assign)

float

height;17

18

/**19

*

初始化的兩個方法20

*21

*/22

-

(instancetype)initWithDict:(NSDictionary

*)dict;23

+

(instancetype)stuWithDict:(NSDictionary

*)dict;24

25

27

28

@end

student.m

-

(instancetype)initWithDict:(NSDictionary

*

(self

=

+

(instancetype)stuWithDict:(NSDictionary

*用一個字典初始化對象

1

-

(void)initStudent

2

{

3

NSDictionary

*dict

=

[NSDictionary

dictionaryWithObjectsAndKeys:

4

@"Tom",@"name",

5

@"110",@"num",

6

@"170.0",@"height",

7

nil];

8

Student

*stu

=

[[Student

alloc]

initWithDict:dict];

9

NSLog(@"name

=

%@

num

=

%@

height

=

%f",,stu.num,stu.height);10

}setValuesForKeyWithDictionary:dict

的原理實際上,setValuesForKeyWithDictionary:dict方法就是遍歷dict,對dict中的每個鍵值調(diào)用setValue:forKey:方法??梢杂孟旅娴姆椒MsetValuesForKeyWithDictionary:dict:1

-

(void)

setProperty:(NSDictionary

*)dict2

{3

for(NSString

*key

in

[dict

allKeys])4

{5

NSString

*value

=

[dict

objectForKey:key];6

[self

setValue:value

forKey:key];7

}8

}調(diào)用時:1

-

(instancetype)initWithDict:(NSDictionary

*)dict{2

if(self

=

[super

init]){3

//[self

setValuesForKeysWithDictionary:dict];4

[self

setProperty:dict];5

}6

return

self;7

}和setValuesForKeyWithDictionary:dict功能是一樣的。使用setValuesForKeyWithDictionary:dict一個需要注意的地方當字典中有某個值,而對象沒有相應(yīng)的屬性時,會發(fā)生崩潰。比如,新的字典如下:

1

-

(void)initStudent

2

{

3

NSDictionary

*dict

=

[NSDictionary

dictionaryWithObjectsAndKeys:

4

@"Tom",@"name",

5

@"110",@"num",

6

@"170.0",@"height",

7

@"boy",@"sex",nil];

8

Student

*stu

=

[[Student

alloc]

initWithDict:dict];

9

NSLog(@"name

=

%@

num

=

%@

height

=

%f",,stu.num,stu.height);10

}字典中有sex屬性,但是Studeng對象中沒有這個屬性,此時會發(fā)生崩潰。解決方法:實現(xiàn)setValue:

forUndefineKey:

方法,在該方法中處理出現(xiàn)沒有屬性的情況。student.h中添加代碼:1

-

(void)setValue:(id)value

forUndefinedKey:(NSString

*)key;student.m中添加代碼:1

-

(void)setValue:(id)value

forUndefinedKey:(NSString

*)key2

{3

4

}當出現(xiàn)沒有屬性的情況時,就會調(diào)用setValue:forUndefineKey:方法。因為該方法沒做處理,所以這種情況不做處理,不會發(fā)生崩潰。需要注意的是:setValue:forUndefineKey:方法用途很廣泛,比如說字典中某個key值為id,但是在OC中id是關(guān)鍵字,這種情況也可以在

setValue:forUndefineKey:方法中處理。三:鍵值監(jiān)聽(KVO)KVO全稱KeyValueObserving。使用KVO可以實現(xiàn)視圖組件和數(shù)據(jù)模型的分離,視圖作為監(jiān)聽器,當模型的屬性值發(fā)生變化后,監(jiān)聽器可以做相應(yīng)的處理。KVO的方法由NSKeyValueObserving協(xié)議提供,同樣NSObject已經(jīng)實現(xiàn)了該協(xié)議,因此幾乎所有的對象都可以使用KVO。使用KVO操作常用的方法如下:注冊制定路徑的監(jiān)聽器:addObserver:

forKeyPath:option:context:刪除制定路徑的監(jiān)聽器:removeObserver:forKeyPath:觸發(fā)監(jiān)聽時的方法:observeValueForKeyPath:ofObject:change:context:

一個KVO的例子:有兩個類:Dog類和People類Dog.h

1

#import

<Foundation/Foundation.h>2

3

@interface

Dog

:

NSObject4

5

@property

(nonatomic,

copy)

NSString

*name;6

7

@end

Dog.m1

#import

"Dog.h"2

3

@implementation

Dog4

5

@endPeople.h

1

#import

<Foundation/Foundation.h>

2

3

@class

Dog;

4

5

@interface

People

:

NSObject

6

7

@property

(nonatomic

,

copy)

NSString

*name;

8

@property

(nonatomic

,

strong)

Dog

*dog;

9

10

@endPeople.m

1

#import

"People.h"

2

#import

"Dog.h"

3

4

@implementation

People

5

6

/**

7

*

初始化時增加對dog的監(jiān)聽

8

*

9

*/10

-

(void)setDog:(Dog

*)dog11

{12

_dog

=

dog;13

[self.dog

addObserver:self

forKeyPath:@"name"

options:NSKeyValueObservingOptionNew

context:nil];14

}15

/**16

*

重寫observeValueForKeyPath方法17

*/18

-

(void)observeValueForKeyPath:(NSString

*)keyPath

ofObject:(id)object

change:(NSDictionary<NSString

*,id>

*)change

context:(void

*)context19

{20

if([keyPath

isEqualToString:@"name"]){21

NSLog(@"newNa

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論