A-IOSHelper [Animax Helper]

This is a scaffold library and the aid of the project is providing all foundation functions for IOS developer to build app easier.
Stories in Ready Build Status


Project maintained by Animaxx Thanks mattgraham for original Theme

Key-Value Observing

KVO helper provides changed notification and objects binding method with blocks.
Note: if your object is using KVO helper and has its own dealloc methods, please put [self A_RemoveObservings] in it.



Contents

The demo model structure


            // TestDataModel
            @interface TestDataModel : A_DataModel
            @property (retain, nonatomic) NSString* Name;
            @property (retain, nonatomic) NSNumber* ID;
            @end

            // ExampleModel
            @interface ExampleModel : NSObject
            @property (strong, nonatomic) TestDataModel* model;
            @property (nonatomic) int times;
            @end
        


Add Observer Example

    /* Add an observer. In the default,
    it's using NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew options. */
    TestDataModel* _model = [[TestDataModel alloc] init];
    [_model A_AddObserver:@"Name" block:^(TestDataModel *itself, NSDictionary *change) {
        XCTAssertTrue([[change objectForKey:@"new"] isEqualToString:@"A"]);
    }];
    [_model setName:@"A"]; 
    // You can also setup your Observing Option
    [_model A_AddObserverWithOption:NSKeyValueObservingOptionNew
             Key:@"ID" block:^(TestDataModel *itself, NSDictionary *change) {
        // [change objectForKey:@"new"] is the new value
    }];
    // With an assist parameter:
    [_model A_AddObserver:@"ID" Param:@"The Message"
                block:^(TestDataModel *itself, NSDictionary *change, id param) {

        [A_TaskHelper A_RunInMainWithParam:param Block:^(id arg) {

            [UIAlertView A_DisplyAlert:[NSString stringWithFormat:@"%@",arg]
                              AndTitle:@"Changed Notice"
                          CancelButton:@"Okay"];

        }];

    }];


Remove Observer Example

    //Remove the observer of Name
    [_model A_RemoveObserver:@"Name"];

    // Remove all observers
    [_model A_RemoveAllObservers];
        


Binding Example

        ExampleModel* example = [[ExampleModel alloc] init];
        [example A_Bind:@"times" To:@"model.ID"];

        example.times = 1;
        // example.model.ID => (NSNumber)(int)1
        // Binding method provides covert block
        ExampleModel* example = [[ExampleModel alloc] init];
        /* the type of covert block is ^id(id value), please modify to corresponding type;
           in this case, it should be ^NSString(int value)*/
        [example A_Bind:@"times" To:@"model.Name" Convert:^NSString(int value) {
            return [NSString stringWithFormat:@"The times is %@",value];
        }];

        example.times = 1;
        // example.model.Name => (NSString)@"The times is 1"
        // Crossing object binding
        ExampleModel* example1 = [[ExampleModel alloc] init];
        ExampleModel* example2 = [[ExampleModel alloc] init];
        [example1 A_Bind:@"model.Name" ToTager:example2 AndKey:@"model.Name"];

        [example1.model setName:@"Test"];
        // example2.model.Name => Same as example1.model.Name (NSString)@"Test" 


Remove Binding Example

    //Remove binding is same as remove Observer
            [_model A_RemoveBinding:@"model.Name"];

            // Remove all binding
            [_model A_RemoveAllBinding];