jamztang
7/16/2013 - 2:13 PM

The simplest KVO+Block wrapper. http://ioscodesnippet.com

The simplest KVO+Block wrapper. http://ioscodesnippet.com

Copyright (c) 2013 Jamz Tang <jamz@jamztang.com>
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Pod::Spec.new do |s|
  s.name         = "JTKeyValueObserver"
  s.version      = "0.0.1"
  s.summary      = "Revisiting KVO+Block, the simplest version."
  s.homepage     = "https://gist.github.com/jamztang/6009092"
  s.author       = { "Jamz Tang" => "jamz@jamztang.com" }
  s.platform     = :ios
  s.source       = { :git => "git://gist.github.com/6009092.git", :tag => '0.0.1' }
  s.requires_arc = true
  s.ios.deployment_target = '5.0'
  s.source_files  = '*.{h,m}'
  s.license      = { :type => 'MIT', :file => 'LICENSE' }
end
/*
 * This file is part of the http://ioscodesnippet.com
 * (c) Jamz Tang <jamz@jamztang.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#import "JTKeyValueObserver.h"

@interface JTKeyValueObserver ()

@property (nonatomic, copy) NSString *keyPath;
@property (nonatomic, copy) JTKVOHandler handlerBlock;
@property (nonatomic, unsafe_unretained) NSObject *object;    // Weak property released before dealloc kicks in, but we don't really want to retain the owner

@end

@implementation JTKeyValueObserver

- (id)initWithObject:(__weak NSObject *)object
             keyPath:(NSString *)keyPath
              option:(NSKeyValueObservingOptions)options
             context:(void *)context
        handlerBlock:(JTKVOHandler)handler {
    
    self = [super init];
    
    if (self) {
        self.keyPath = keyPath;
        self.object = object;
        self.handlerBlock = handler;
        [object addObserver:self
                 forKeyPath:keyPath
                    options:options
                    context:context];
    }
    
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (self.handlerBlock) {
        self.handlerBlock(self.object, keyPath, change, context);
    }
}

- (void)dealloc {
    [self.object removeObserver:self forKeyPath:self.keyPath];
}

@end
/*
 * This file is part of the http://ioscodesnippet.com
 * (c) Jamz Tang <jamz@jamztang.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/*
 * Usage:
 *
 * // Create a property 
 * @property (nonatomic, strong) JTKeyValueObserver *keyObserver;
 * 
 * // Just use the initializer, and you don't need to deregister any observers
 * // because the wrapper does it for you when it's released
 *
 - (void)viewDidLoad {
      [super viewDidLoad];

      self.keyObserver = [[JTKeyValueObserver alloc] initWithObject:self
                                                            keyPath:@"state"
                                                             option:NSKeyValueObservingOptionNew
                                                            context:nil
                                                       handlerBlock:^(NSObject *object, NSString *keyPath, NSDictionary *change, void *context) {
                                                           NSLog(@"%@", [object valueForKeyPath:keyPath]);
                                                       }];
  }
 */


#import <Foundation/Foundation.h>

typedef void(^JTKVOHandler)(NSObject *object, NSString *keyPath, NSDictionary *change, void *context);

@interface JTKeyValueObserver : NSObject

- (id)initWithObject:(__weak NSObject *)object
             keyPath:(NSString *)keyPath
              option:(NSKeyValueObservingOptions)options
             context:(void *)context
        handlerBlock:(JTKVOHandler)handler;

@end