Returns a new expression that will invoke one of the predefined functions.
SDKs
- iOS 3.0+
- macOS 10.4+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
init(forFunction name: String, arguments parameters: [Any])
Parameters
name
The name of the function to invoke.
parameters
An array containing
NSExpression
objects that will be used as parameters during the invocation of selector.For a selector taking no parameters, the array should be empty. For a selector taking one or more parameters, the array should contain one
NSExpression
object which will evaluate to an instance of the appropriate type for each parameter.If there is a mismatch between the number of parameters expected and the number you provide during evaluation, an exception may be raised or missing parameters may simply be replaced by
nil
(which occurs depends on how many parameters are provided, and whether you have over- or underflow).
Return Value
A new expression that invokes the function name
using the parameters in parameters
.
Discussion
The name
parameter can be one of the following predefined functions.
Function |
Parameter |
Returns | Availability |
---|---|---|---|
|
An |
An | OS X v10.4 and later |
|
An |
An | OS X v10.4 and later |
|
An |
An | OS X v10.4 and later |
|
An |
An | OS X v10.4 and later |
| An | An | OS X v10.4 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
|
| An | OS X v10.5 and later |
| An | An | OS X v10.5 and later |
|
| An | OS X v10.5 and later |
| An | An | iOS 3.0 and later |
| An | An | iOS 3.0 and later |
| An | An | iOS 3.0 and later |
| An | An | iOS 3.0 and later |
| An | An | iOS 3.0 and later |
| An | An | iOS 3.0 and later |
| An | An | iOS 3.0 and later |
| An | An | iOS 3.0 and later |
| An | An | iOS 3.0 and later |
| An | The result of evaluating the parameter as though the | iOS 3.0 and later |
This method raises an exception immediately if the selector is invalid; it raises an exception at runtime if the parameters are incorrect.
The parameters
argument is a collection containing an expression which evaluates to a collection, as illustrated in the following examples:
NSNumber *number1 = [NSNumber numberWithInteger:20];
NSNumber *number2 = [NSNumber numberWithInteger:40];
NSArray *numberArray = [NSArray arrayWithObjects: number1, number2, nil];
NSExpression *arrayExpression = [NSExpression expressionForConstantValue: numberArray];
NSArray *argumentArray = [NSArray arrayWithObject: arrayExpression];
NSExpression* expression =
[NSExpression expressionForFunction:@"sum:" arguments:argumentArray];
id result = [expression expressionValueWithObject: nil context: nil];
BOOL ok = [result isEqual: [NSNumber numberWithInt: 60]]; // ok == YES
[NSExpression expressionForFunction:@"random" arguments:nil];
[NSExpression expressionForFunction:@"max:"
arguments: [NSArray arrayWithObject:
[NSExpression expressionForConstantValue:
[NSArray arrayWithObjects:
[NSNumber numberWithInt: 5], [NSNumber numberWithInt: 10], nil]]]];
[NSExpression expressionForFunction:@"subtract:from:"
arguments: [NSArray arrayWithObjects:
[NSExpression expressionForConstantValue: [NSNumber numberWithInt: 5]],
[NSExpression expressionForConstantValue: [NSNumber numberWithInt: 10]], nil]];
Special Considerations
This method throws an exception immediately if the selector is unknown; it throws at runtime if the parameters are incorrect.