Home>

Analysis of kvo examples

Recently encountered a problem,When working on a comment interface in a project,Because directly using uiwebview to display the list of comments,As a result, there were a mess of ads with a cgsize of (320, 65) above and below the page.Very annoying.The head advertisement is solved because it is convenient to put your own logo on the head coordinates, but the tail,Because the reviews on each page vary in length,The coordinates are different,In this way, you cannot give the dead coordinates to paste the logo.This problem is solved very well by kvo.

@kvo overview:

kvo, ie:key-value observing, it provides a mechanism,When the properties of the specified object are modified,The object will receive the notification.

Simply put, every time the properties of the specified observed object are modified,kvo will automatically notify the corresponding observer.

The steps are as follows:

1. Registration, specifying the attributes of the observed person,

2. Implement the callback method

3. Trigger the callback method

4. Remove observation

Code example:

-(void) viewdidload {
 //kvo, as an observer,As long as the property "contentsize" changes,It will be notified in the callback method
 [_webview.scrollview addobserver:self forkeypath:@"contentsize" options:nskeyvalueobservingoptionnew context:null];
}
//callback method
-(void) observevalueforkeypath:(nsstring *) keypath ofobject:(id) object change:(nsdictionary *) change context:(voidvoid *) context
{
 if (object == _webview.scrollview&&[keypath isequaltostring:@"contentsize"])
 {
 //get the largest y coordinate
 cgsize size=_webview.scrollview.contentsize;
 if (size.height>568.0) {
 //block ads
 _hidebottomimage=[[uiimageview alloc] initwithframe:cgrectmake (0, size.height-67, screenwidth, 67)];
 _hidebottomimage.image=[uiimage imagenamed:@"banner"];
 [_webview.scrollview addsubview:_hidebottomimage];
 [_hidebottomimage release];
 }
 }
 else
 {
 //call the parent method
 [super observevalueforkeypath:keypath ofobject:object change:change context:context];
 }
}
-(void) dealloc {//---->The dealloc method can also be called in the arc environment, but there is no need to write [super dealloc]
 //remove kvo, otherwise it will cause resource leakage
 [_webview.scrollview removeobserver:self forkeypath:@"contentsize"];
 [super dealloc];
}

The above is for the contentsize property, and so on for other properties

kvc

Generally, we use the set and get methods of properties to assign and get values.Here we introduce the use of key-value-coding (kvc) key-value encoding to assign and value properties of the class.

1. The basic way (setvalue:forkey:valueforkey)

//--- Define a student class (.m file does nothing)
#import<foundation/foundation.h>
@class hmtclass;
@interface hmtstudent:nsobject {
 nsstring * _name;
 bool _test;
 bool _istest;
 bool test;
 bool istest;
}
@property (nonatomic, copy) nsstring * name;
@property (nonatomic, copy) nsstring * sex;
@property (nonatomic, assign) nsinteger age;
@property (nonatomic, strong) hmtclass * hmtclass;
@end
//--- main file
hmtstudent * student=[[hmtstudent alloc] init];
student.hmtclass=[[hmtclass alloc] init];
student.name [email protected]"humingtao";//set method assignment
//kvc assignment
[student setvalue:@"mawei is dog" forkey:@"name"];
[student setvalue:@"m" forkey:@"sex"];
[student setvalue:@(10) forkey:@"age"];
//value

pay attention:

I also defined 4 bool value variables in the class to verify the order of kvc access attribute keys

[student setvalue:@(yes) forkey:@"test"];

The result is:_test —>_ istest—>test—>istest

2. Key path access (property setvalue:forkeypath:forkeypath for attributes in a class)

//create a class
@interface hmtclass:nsobject
@property (nonatomic, copy) nsstring * name;
@end

Then I wrote a class attribute hmtclass in the student class in the first point

hmtclass * hmtclass=[[hmtclass alloc] init];
[hmtclass setvalue:@"Universe class" forkey:@"name"];
[student setvalue:hmtclass forkey:@"hmtclass"];
nsstring * hmtclassname=[student valueforkeypath:@"hmtclass.name"];
//You can also store the value like this
[student setvalue:@"Universe class" forkeypath:@"hmtclass.name"];
student.hmtclass.name=[student valueforkeypath:@"hmtclass.name"];

3. Automatically encapsulate basic data types

We add the fraction attribute nsinteger number to the student class;

#import<foundation/foundation.h>
@class hmtclass;
@interface hmtstudent:nsobject
{
 nsstring * _name;
 nsinteger number;
}
@end
[student setvalue:@"100" forkeypath:@"number"];
nsstring * number=[student valueforkey:@"number"];

It can be seen that the property [email protected]"100" set with the nsstring * type, and our property is of the nsinteger type, and there is no problem in accessing it.

4. Operation collection

Add the array nsarray to the student class to represent other students.

#import<foundation/foundation.h>
@class hmtclass;
@interface hmtstudent:nsobject
{
 nsarray * manystudents;
}
@end
student * student1=[[hmtstudent alloc] init];
student * student2=[[hmtstudent alloc] init];
student * student3=[[hmtstudent alloc] init];
[student1 setvalue:@"200" forkey:@"number"];
[student2 setvalue:@"300" forkey:@"number"];
[student3 setvalue:@"400" forkey:@"number"];
nsarray * array=[nsarray arraywithobjects:student1, student2, student3, nil];
[student setvalue:array forkey:@"manystudents"];
nslog (@ "%@", [student valueforkeypath:@"manystudents.number"]);

Printed out is an array (200, 300, 400)

  • Previous Solution to recover sql server 2000 accidentally deleted data
  • Next PHP file extension judgment and N ways to get file extension