Generate KeyPaths and Keys

 

If you generate keyPaths, you can use them for observing of for logging. You can generate the code by including them in your Implementation file generation turning on the switch in the main interface, or easier, just use the Action Menu or Action Panel actions Description for keys, and Generate keys.

Example: Let’s say we have these properties and we want to generate a

Description for keys based on these properties.

- (NSArray *)keyPaths {

    NSArray *result = [NSArray arrayWithObjects:

                       @"measureSection",

                       @"relativeNote",

                       @"playersView",

                       @"showKeysSwitch",

                       @"defaultAutoPlay",

                       @"startTime",

                       @"playHour",

                       @"inReverse",

                       @"mArray",

                       @"mSet",

                       nil];

   

    return result;

}


- (NSString *)descriptionForKeyPaths  {

    NSMutableString *desc = [NSMutableString string];

    [desc appendString:@"\n\n"];

    [desc appendFormat:@"Class name: %@\n", NSStringFromClass([self class])];

   

    NSArray *keyPathsArray = [self keyPaths];

    for (NSString *keyPath in keyPathsArray) {

        [desc appendFormat: @"%@: %@\n", keyPath, [self valueForKey:keyPath]];

    }

   

    return [NSString stringWithString:desc];

}

Results will look like this:

Once you’ve created these two methods, you can then call -descriptionForKeyPaths on your class and it will provide you values for any key that is in the array.  This is a handy way of logging values or even using them for other purposes.  NOTE: you get the class name included - allowing you to make requests from other classes.

If you want Key-Value Observing, then turn these switches on in the main Accessorizer Style panel:

- (void)startObservingObject:(id)thisObject {

    if ([thisObject respondsToSelector:@selector(keyPaths)]) {

        NSArray *keyPathsArray = [thisObject keyPaths];

        for (NSString *keyPath in keyPathsArray) {

            [thisObject addObserver:self

                         forKeyPath:keyPath

                            options:NSKeyValueObservingOptionOld

                            context:NULL];

        }

    }

}

- (void)stopObservingObject:(id)thisObject {

    if ([thisObject respondsToSelector:@selector(keyPaths)]) {

        NSArray *keyPathsArray = [thisObject keyPaths];

        for (NSString *keyPath in keyPathsArray) {

            [thisObject removeObserver:self forKeyPath:keyPath];

        }

    }

}

And the results will include the following observing methods (even when generated from the Action Menu or Action Panel.

As you add more keys to your class, you can easily generate the key and insert it into the array using Generate Keys (which works both ivars and properties).

    @"ready",

    @"noteState",

    @"noteAddress",


NOTE: if the -(NSArray *)keyPaths switch is OFF, but the observing switches are checked ON, the ACTIONS from the Action Menu and Action Panel will include the observing methods.

NOTE: You can define your key-value pair separator in the Coding Style -> Formatting Methods panel:

You can set this value to \t or spaces or a period... etc.  Be sure to hit the ENTER key to trigger the didEndEditing notification.