Re: re Boolean Predicates
Re: re Boolean Predicates
- Subject: Re: re Boolean Predicates
- From: Nick Zitzmann <email@hidden>
- Date: Sun, 31 Oct 2004 07:44:39 -0700
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:
This email sent to email@hidden