Re: NSExpression
Re: NSExpression
- Subject: Re: NSExpression
- From: Nathan Kinsinger <email@hidden>
- Date: Sun, 29 Jun 2008 02:38:23 -0600
On Jun 28, 2008, at 11:35 PM, Chris wrote:
If anyone has a clue how to use it, I'd be grateful. This was my
unsuccessful attempt:
NSExpression * ex = [NSExpression expressionForFunction:
[NSExpression expressionForConstantValue:@"BAR"]
selectorName:@"length" arguments:nil];
NSPredicate * predicate = [NSCompoundPredicate
andPredicateWithSubpredicates:[NSArray arrayWithObject: ex]];
[predicate evaluateWithObject:@"FOO" substitutionVariables:nil];
[NSFunctionExpression evaluateWithObject:substitutionVariables:]:
unrecognized selector sent to instance 0x68b8af0
1) If you want to evaluate the NSExpression then use
expressionValueWithObject:context.
NSExpression * ex = [NSExpression expressionForFunction:[NSExpression
expressionForConstantValue:@"BAR"] selectorName:@"length"
arguments:nil];
int result = (int)[ex expressionValueWithObject:nil context:nil];
but this is probably not what you were trying to do (you didn't say
what you were trying to do so I'm guessing). This is also a rather
long way to get the length of a string.
2) andPredicateWithSubpredicates: wants an array of NSPredicates not
NSExpressions. When you evaluated the NSPredicate the NSExpression
object you put in the subpredicate array does not implement
evaluateWithObject:substitutionVariables: and you get that warning.
(Note: NSFunctionExpression is a subclass of NSExpression created for
expressions of type NSFunctionExpressionType)
I'm going to guess that you want to use a predicate to check the
length of one string to the length of several others?
One way using a NSPredicate could be:
NSString *bar = [NSString stringWithString:@"BAR"];
NSString *foo = [NSString stringWithString:@"FOO"];
NSString *foobar = [NSString stringWithString:@"FOOBAR"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"length ==
%d", [bar length]];
BOOL result1 = [predicate evaluateWithObject:foo];
NSLog(@"%@ = %@", foo, result1 ? @"YES" : @"NO");
BOOL result2 = [predicate evaluateWithObject:foobar];
NSLog(@"%@ = %@", foobar, result2 ? @"YES" : @"NO");
output:
2008-06-29 02:23:23.475 testStrings[68040:10b] FOO = YES
2008-06-29 02:23:23.485 testStrings[68040:10b] FOOBAR = NO
There are easier ways to compare the lengths (just use the length
method in an if statement).
If this does not cover what you are trying to do then you need to give
more info.
--Nathan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden