Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: re Boolean Predicates




On Oct 31, 2004, at 7:28 AM, John James wrote:

I probably don't understand the Objective-C features, but I am trying to iterate over a list of objects (from the rear) and discover the first one to satisfy a given boolean expression.
my code is:

@interface VectorOfStates : NSMutableArray

1. If you try subclassing NSMutableArray, then the program will most likely raise an exception whenever a "VectorOfStates" object is used. You can not easily subclass many objects in the Foundation framework, including NSArray, NSSet, NSString, NSDictionary, NSNumber, and a few others because they are class clusters.

{
...
}
-(State*) FindLastIf:(SEL) aPred;

2. Objective-C methods should begin with a lower case character.

...
@end

// find last location on statestack
-(State*) FindLastIf:(SEL) aPred
{
NSEnumerator *enumerator = [self reverseObjectEnumerator];
id object;
while (object = [enumerator nextObject])
{
if((BOOL)[object aPred]) <------------------------- warning here

3. You're getting a warning because you have no objects that respond to a method called "aPred". I'm guessing you want to perform the selector "aPred" and get the result. Unfortunately you'll have to use the NSInvocation class to do that, since there are no built-in methods that invoke a selector and return a boolean. Something like this ought to work: (warning - written in Mail, untested, use at your own risk, etc.)

BOOL returnValue;
NSInvocation *invoc = [NSInvocation invocationWithMethodSignature:[object methodSignatureForSelector:aPred]];

[invoc setSelector:aPred];
[invoc setReturnValue:&returnValue];
[invoc invokeWithTarget:object];

The -invokeWithTarget: method will call the selector, and the return value will be in the returnValue variable. See the NSInvocation documentation for more details.

Nick Zitzmann
<http://www.chronosnet.com/>

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >re Boolean Predicates (From: John James <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.