在iOS中,有两种技术是属于观察者模式的。
它们分别是通知和KVO。
通知
NSNotificationCenter *notificantionCenter = [NSNotificationCenter defaultCenter];
[notificantionCenter addObserver:self selector:@selector(update) name:@"notifiName" object:nil];
发送通知
NSNotification *nofitication = [NSNotification notificationWithName:@"nofitiName" object:nil];
[notificantionCenter postNotification:nofitication];
KVO
[student addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
回调方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void*)context{
}