LoginSignup
36
35

More than 5 years have passed since last update.

KVOやるならFBKVOController使うべき

Posted at
  • シンプルで、モダンなkey value observingが行える

メリット

  • ブロックで書けるので、コードが読みやすくなる
  • observerの削除のときにexceptionが起きない
  • observerの削除はFBKVOControllerのdelloc時に呼ばれる

使い方

// create KVO controller with observer
FBKVOController *KVOController = [FBKVOController controllerWithObserver:self];

// observe clock date property
[KVOController observe:clock keyPath:@"date" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew block:^(ClockView *clockView, Clock *clock, NSDictionary *change) {

  // update clock view with new value
  clockView.date = change[NSKeyValueChangeNewKey];
}];
  • 実際にはviewやcontrollerのdelloc時に呼ぶために以下のように書く
// Observer with KVO controller instance variable
@implementation ClockView
{
  FBKVOController *_KVOController;
}

- (id)init
{
  ...
  // create KVO controller with observer
  FBKVOController *KVOController = [FBKVOController controllerWithObserver:self];

  // add strong reference from observer to KVO controller
  _KVOController = KVOController;

インストール

pod 'KVOController'

参考

36
35
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
35