Giter Club home page Giter Club logo

keypathobserver's Introduction

About this class

This category helps you add KVO using blocks to make it easier to use and make your code more readable.

Associate a Objective-C block to a keyPath, and your block will be executed each time the value of the KeyPath is changed to let you do whatever you want. No need to centralize everything in -(void)observeValueForKeyPath:ofObject:change:context:, and no need for removeObserver:forKeyPath: anymore either as the observer blocks are automatically removed when the object is deallocated!

Usage examples

Example 1: observing a CGRect property

[self onKeyPathValueChange:@"frame"
 execute:^(id obj, NSString* kp, id old, id new)
 {
    if (![old isEqual:new])
    {
      NSLog(@"The frame changed from %@ to %@", old, new);
      CGRect newFrame = [new CGRectValue];
      ...
    } else {
      NSLog(@"The frame property was reaffected to the same CGRect value %@", new);
    }
 }];

Example 2: Observing an UIColor object and using a composite keyPath

[self onKeyPathValueChange:@"view.backgroundColor"
 execute:^(id obj, NSString* kp, id old, id new)
 {
    if (old != new)
    {
      NSLog(@"The view background color has been changed");
    }
 }];

Additional note: avoid retain cycles

Be careful to avoid retain cycles when using this helper method (as with any other usage of blocks retained by self).

Especially, the blocks you provide to onKeyPathValueChange:execute: are retained by self, so you should not use self directly in the block to avoid self to be retained by the block and creating a retain cycle.

Instead, you should declare a weak non-retaining reference to self like this before your block:

__block __weak typeof(self) weakSelf = self;

And use weakSelf instead of self in the block body, so that self won't be retained by the block.

keypathobserver's People

Contributors

alisoftware avatar

Watchers

James Cloos avatar weizheng Li avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.