Advertisements
NSExpression by example
Initializing an Expression
+ expressionWithFormat:
Initializes the receiver with the specified expression arguments.
Example
NSExpression *expression = [NSExpression expressionWithFormat: @"%d*%f",3,3.5];; NSLog(@"%@",[expression expressionValueWithObject:nil context:nil]);
Output
2014-04-12 06:24:58.061 iOS-Tutorial[2064:a0b] 10.5
+ expressionWithFormat:argumentArray:
Initializes the receiver with the specified expression format and array of arguments.
Example
NSExpression *expression = [NSExpression expressionWithFormat: @"%f*%f" argumentArray:@[@3.4,@3.1]];; NSLog(@"%@",[expression expressionValueWithObject:nil context:nil]);
Output
2014-04-12 06:27:06.855 iOS-Tutorial[2093:a0b] 10.54
Creating an Expression for a Value
+ expressionForConstantValue:
Returns a new expression that represents a given constant value.
Example
NSExpression *expression = [NSExpression expressionForConstantValue:[NSNumber numberWithDouble:4.2]]; NSLog(@"%@",[expression expressionValueWithObject:nil context:nil]);
Output
2014-04-12 07:23:25.328 iOS-Tutorial[2262:a0b] 4.2
Creating an Expression for a Function
+ expressionForFunction:arguments:
Returns a new expression that will invoke one of the predefined functions.
Example
NSExpression *now = [NSExpression expressionForFunction:@"now" arguments:[NSArray array]]; NSLog(@"%@",[now expressionValueWithObject:nil context:nil]);
Output
2014-04-12 06:50:00.006 iOS-Tutorial[2145:a0b] 2014-04-12 01:20:00 +0000
+ expressionForFunction:selectorName:arguments:
Returns an expression which will return the result of invoking on a given target a selector with a given name using given arguments.
Example
NSExpression *expression = [NSExpression expressionForConstantValue:[NSNumber numberWithDouble:4.2]]; NSExpression *functionalExpression = [NSExpression expressionForFunction:expression selectorName:@"stringValue" arguments:nil]; NSLog(@"%@",[functionalExpression expressionValueWithObject:nil context:nil]);
Output
2014-04-12 07:23:25.330 iOS-Tutorial[2262:a0b] 4.2
Getting Information About an Expression
- arguments
Returns the arguments for the receiver.
Example
NSExpression *now = [NSExpression expressionForFunction:@"now" arguments:[NSArray array]]; NSLog(@"%@",[now arguments]);
Output
2014-04-12 08:07:21.659 iOS-Tutorial[2327:a0b] ( )
- constantValue
Returns the constant value of the receiver.
Example
NSExpression *expression = [NSExpression expressionForConstantValue:[NSNumber numberWithDouble:4.2]]; NSLog(@"%@",[expression constantValue]);
Output
2014-04-12 08:09:29.514 iOS-Tutorial[2377:a0b] 4.2
- expressionType
Returns the expression type for the receiver.
Example
NSExpression *expression = [NSExpression expressionForConstantValue:[NSNumber numberWithDouble:4.2]]; NSLog(@"%@",[expression expressionType]==NSConstantValueExpressionType?@"NSConstantValueExpressionType":@"Not NSConstantValueExpressionType");
Output
2014-04-12 08:11:22.417 iOS-Tutorial[2409:a0b] NSConstantValueExpressionType
- function
Returns the function for the receiver.
Example
NSExpression *now = [NSExpression expressionForFunction:@"now" arguments:[NSArray array]]; NSLog(@"%@",[now function]);
Output
2014-04-12 08:12:08.078 iOS-Tutorial[2423:a0b] now
Advertisements