Giter Club home page Giter Club logo

kvomutablearray's Introduction

KVOMutableArray

KVOMutableArray is a proxy object which supports key value observation of NSMutableArray.

Coverage Status CI Status Coverage Status Version License Platform

Installation

KVOMutableArray is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "KVOMutableArray"

KVOMutableArray supports monitoring with ReactiveCocoa signals. To enable this feature, add the following line instead:

pod "KVOMutableArray/ReactiveCocoaSupport"

If you don't have CocoaPods installed or integrated into your project, you can learn how to do so here.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

So what can I do with it?

Receive KVO notificaiton, YEAH!

KVOMutableArray* array = [KVOMutableArray new];
AMBlockToken* token = [array addObserverWithTask:^BOOL(id obj, NSDictionary *change) {
        NSIndexSet *indexes = change[NSKeyValueChangeIndexesKey];
        NSNumber *kind = change[NSKeyValueChangeKindKey];
        NSArray* new = change[NSKeyValueChangeNewKey];
        NSArray* old = change[NSKeyValueChangeOldKey];
        
        if ([kind integerValue] == NSKeyValueChangeInsertion)
        {
            NSLog(@"objects %@ added at indexes: %@", new, indexes);
        } else if ([kind integerValue] == NSKeyValueChangeRemoval)
        {
            NSLog(@"objects %@ removed from indexes: %@", old, indexes);
        }

        return YES;
    }];

Usage

Init array

KVOMutableArray* array = [KVOMutableArray new];
[array addObject:@"hello"];
[array addObject:@"world"];

Alternatively, you can do

KVOMutableArray* array = [[KVOMutableArray alloc] initWithObjects:@(1), @(2), @(3), nil];

Init from the exisiting NSArray

NSArray* someArray = @[@(1), @(2), @(3), @(4)];
KVOMutableArray* array = [[KVOMutableArray alloc] initWithArray:someArray];

Register KVO events

AMBlockToken* token = [array addObserverWithTask:^BOOL(id obj, NSDictionary *change) {
    // handle the event here
}];

Alternatively, you can use ReactiveCocoa signals.

RACSignal* signal = [array changeSignal];
[signal subscribeNext:^(RACTuple* t) {
    NSArray *obj = t.first;
    NSDictionary *change= t.second;
    // handle the event here
        
}];

Stop receiving KVO events

[token removeObserver];

Retrieve the enclosing NSMutableArray object

NSMutableArray* theMutableArray = array.arr;

Add, Remove, Access the objects

Manipulating the objects from KVOMutableArray

KVOMutableArray* array = [[KVOMutableArray alloc] initWithArray:@[@"hello", @"world"]];
[array removeLastObject];
[array addObject:@"awesome!"];
[array removeObjectAtIndex:0];
NSString* awesome = array[0];

Or from the enclosing NSMutableArray

KVOMutableArray* array = [[KVOMutableArray alloc] initWithArray:@[@"hello", @"world"]];
[array.arr removeLastObject];
[array.arr addObject:@"awesome!"];
[array.arr removeObjectAtIndex:0];
NSString* awesome = array.arr[0];

Gotchas

KVOMutableArray is now a subclass of NSMutableArray after 1.0 release. This change makes two gotchas available:

First, the unarchived object is an NSMutableArray, not a KVOMutableArary. No solution on StackOverflow so far.

KVOMutableArray* array = [[KVOMutableArray alloc] initWithArray:@[@(1), @(2), @(3)]];
NSData* data = [NSKeyedArchiver archivedDataWithRootObject:array];

// not a KVOMutableArray
NSMutableArray* unarchived = [NSKeyedUnarchiver unarchiveObjectWithData:data];

Second, the object returns from copy is a NSArray. There is no immutable KVOMutableArray, the only reasonable choice is to return immutable NSArray object.

KVOMutableArray* array = [[KVOMutableArray alloc] init];

// not a KVOMutableArray
NSArray* copy = [array copy];

Requirements

Requires iOS 8.0, and ARC.

How it works

Because NSMutableArray cannot be directly key value observed, we monitor the change events from a property of KVOMutableArray. For more details, check the stackoverflow discussion.

Contributing

Forks, patches and other feedback are welcome.

License

KVOMutableArray is available under the MIT license. See the LICENSE file for more info.

kvomutablearray's People

Contributors

haifengkao avatar expkzb 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.