About KVO, estimated that many students on the same kvo is very vague, have heard everyone said, interview will ask, but rarely used in development, the general use of a component is also used in encapsulation, and this package is not to be done by themselves, so it is still very vague. Now, it seems that I do not know, in the end what KVO is, how to use, and what kind of scenes to use. With doubt, let's study together. 1, what is KVO? KVO is actually key-value-observer, that is, the key value Observer, is based on the KVC mechanism to implement another mode. Perhaps a lot of students have used Svpulltorefresh or Mjrefresh refresh components, have used the KVO to achieve. 2, use the scene will use KVO? It seems that the use of the scene is not many, Nsoperationqueue used in the KVO, when the Nsoperation object state changes (finished,canceled, etc.), the observer can know the change, so that the corresponding processing (removal of completed and so on); 3, How to achieve KVO? Now suppose the need: When a student's grades change, let the observer know that let's create a student entity class and implement the file without moving:
@interface HYBStudent : NSObject@property (nonatomicNSString *studentName;@property (nonatomicassignCGFloat grade;@end
A student entity is instantiated in the following Viewcontroller, which makes the controller The observer of the student entity, and here is a one-off observation mode to see the printout:
#import "ViewController.h" #import "HYBStudent.h" @interface viewcontroller ()@property(nonatomic,Strong) Hybstudent *student;@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload]; Self. Student= [[Hybstudent alloc] init]; Self. Student. Studentname= @"John Doe"; Self. Student. Grade=.F//Add observer to the student[ Self. StudentAddobserver: Selfforkeypath:@"Grade"Options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptioninitial Context:Nil]; [ SelfPerformselector:@selector(Changegrade) Withobject:NilAfterdelay:4.0];} - (void) Changegrade { Self. Student. Grade=100.0F;} - (void) Observevalueforkeypath: (NullableNSString*) KeyPath Ofobject: (NullableID) Object change: (Nullablensdictionary*) Change context: (Nullablevoid*) Context {if([KeyPath isequaltostring:@"Grade"]) {NSLog(@"Student%@ ' s grade is%f '", Self. Student. Studentname, Self. Student. Grade); }}- (void) Dealloc {//Don ' t forget to remove observer[ Self. StudentRemoveobserver: Selfforkeypath:@"Grade"ContextNil];}@end
The printing results are as follows:
2015 - -28 16 : 18 : 00.537 kvodemo[1281 : 476192 ] student John Doe ' s grade is 90.000000 ' 2015 -08 -28 16 : 18 : 04.539 kvodemo[1281 : 476192 ] student John Doe ' s grade is 100.000000 '
Let's take a look at another scenario: a one-to-many requirement is as follows: When students John Doe grades change, to inform his classmates Zhang San implement the following, in the HYBSTUDENT.M file to implement the following methods:
- (voidNSString *)keyPath id)object NSDictionary *)change void *)context { if ([keyPath isEqualToString:@"grade"]) { NSLog(@"张三的同学成绩发生了变化: %@‘s grade is %f‘", ((HYBStudent *)object).studentName, ((HYBStudent *)object).grade); }}
Then add the following code at the end of the Viewdidload, and the rest is unchanged:
// 再次实例化一个学生实体,作为student的同学 self.zhangStudent = [[HYBStudent alloc] init]; self.zhangStudent.studentName = @"张三"; self.zhangStudent.grade = 59; [self.student addObserver:self.zhangStudent forKeyPath:@"grade" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial context:nil];
Look at the print results:
-- ,- - -: +:51.234kvodemo[1317:480175] Student John Doe' sGrade is 90.000000‘ -- ,- - -: +:51.235kvodemo[1317:480175] Zhang San's students ' grades have changed: John Doe' sGrade is 90.000000‘ -- ,- - -: +:55.236kvodemo[1317:480175] Zhang San's students ' grades have changed: John Doe' sGrade is 100.000000‘ -- ,- - -: +:55.237kvodemo[1317:480175] Student John Doe' sGrade is 100.000000‘
Summary: 1, who becomes the Observer, who has to realize
- (void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context;
This API, so that when the value changes, do the appropriate processing, if not implemented this API, will throw an exception, leading to crash. 2, add observer and remove observer is in pairs appear, must remember to remove
// Don‘t forget to remove observer [self.student removeObserver:self forKeyPath:@"grade" context:nil]; [self.student removeObserver:self.zhangStudent forKeyPath:@"grade" context:nil];
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Learning and summarization KVO