Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Implementing A Method for Evaluating Object Specifiers

Container classes that want to evaluate certain object specifiers on their own should implement the indicesOfObjectsByEvaluatingObjectSpecifier: method defined by NSScriptObjectSpecifiers (a category on NSObject). For example, you might choose to implement this method if you find that whose clause evaluation is too slow and you want to do your own evaluation to speed it up (though for most applications, performance of the default whose mechanism should be sufficient).

If this method returns nil, the object specifier method for the class does its own evaluation. If this method returns an array, the object specifier uses the NSNumber objects in the array as the indices of the specified objects.

Therefore, if you implement this method and when you evaluate the specifier there are no objects that match, return an empty array, not nil. If you find only one object, you return its index in an array. Returning an array with a single index where the index is –1 is interpreted to mean all the objects match.

The Sketch application implements this method in its document class, as shown in Listing 6-3. This allows Sketch to directly handle some range and relative specifiers for graphics, so it can support script statements such as graphics from circle 3 to circle 5, circles from graphic 1 to graphic 10, or circle before rectangle 3.

Listing 6-3  indicesOfObjectsByEvaluatingObjectSpecifier: method from Sketch

- (NSArray *)indicesOfObjectsByEvaluatingObjectSpecifier:(NSScriptObjectSpecifier *)specifier {
    if ([specifier isKindOfClass:[NSRangeSpecifier class]]) {// 1
        return [self indicesOfObjectsByEvaluatingRangeSpecifier:(NSRangeSpecifier *)specifier];
    } else if ([specifier isKindOfClass:[NSRelativeSpecifier class]]) {// 2
        return [self indicesOfObjectsByEvaluatingRelativeSpecifier:(NSRelativeSpecifier *)specifier];
    }
    return nil;// 3
}

Here’s a description of how this method works:

  1. If the passed specifier is a range specifier, it returns the result of invoking another Sketch method to evaluate the specifier.

    The method indicesOfObjectsByEvaluatingRangeSpecifier: allows more flexible range specifiers to be used with the different graphic keys of a SKTDrawDocument.

    You can examine this method in Sketch. Describing it in full is beyond the scope of this discussion.

  2. If the passed specifier is a relative specifier, it returns the result of invoking another Sketch method to evaluate the specifier.

    The method indicesOfObjectsByEvaluatingRelativeSpecifier: allows more flexible relative specifiers to be used.

    Again, this method is available in Sketch, but is beyond the scope of this discussion.

  3. For any other type of specifier, this method returns nil so that the default object specifier evaluation will take place.



< Previous PageNext Page > Hide TOC


Last updated: 2008-03-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice