How to map XML (objects) to the method in Objective-C?

I have an xml file containing a structure. In this structure, I have an Actions node. Therefore, under the Actions node, there are multiple “Action” nodes, and each node has Value and Name attributes.

For example

< br />













< br />


What I want to do is; I want to map these operations to the methods I implemented in Objective-C.< /p>

For example, I have a class called “CalculatorActions” and define 5 methods. I want to map my actions in xml (text format) to the methods I created in CalculatorActions .

For example

@interface CalculatorActions: NSObject

// Property
@property (strong, nonatomic) NSString* actionScript;

// Actions
- (void)enterFirstOperand:(double)operand;

- (void)enterSecondOperand:(double)operand;< br />
- (void)selectOperator:(NSString*)operator;

- (void)clickCalculate;

// Validations

-(void)validateResult:(NSString*)exptectedResult;

@end

So when I read the xml file, I want to map the operation in the xml file to The corresponding method in the class.

I think what I am looking for is this;

@interface CalculatorActions

[ Action("addOperand", "Enter first operand")]
- (void) addOperand:(double)operand1 ToOther:(double)operand2;

What is the best way?

You can create NSInvocation instances, set selectors, parameters, and optionally capture return values. You You can create all of these using strings.

For example

SEL mySelector = NSSelectorFromString(@"testAddFunction");
Class MyClass = NSClassFromString(@"CalculatorActions");
NSString *myArgument = @"5";

NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:[MyClass instanceMethodSignatureForSelector:mySelector]];
[invocation setTarget:myClass];
[invocation setSelector:mySelector];
[invocation setArgument:&myArgument atIndex:2];
[invocation invoke];

Note – setArgument: The selector accepts the pointer address, and the parameter index starts from 2.

I have an xml file containing a structure. In this structure, I have an Actions node. Therefore, Under the Actions node, there are multiple “Action” nodes, and each node has Value and Name attributes.

For example



















What I want to do is; I want to map these operations to the methods I implemented in Objective-C.

For example, I have a class called “CalculatorActions” and define 5 methods. I want to map my actions in xml (text format) to the methods I created in CalculatorActions.

For example

@interface CalculatorActions: NSObject

// Property
@property (strong, nonatomic) NSString* actionScript;
< br />// Actions
- (void)enterFirstOperand:(double)operand;

- (void)enterSecondOperand:(double)operand;

- ( void)selectOperator:(NSString*)operator;

- (void)clickCa lculate;

// Validations

-(void)validateResult:(NSString*)exptectedResult;

@end

So when I read the xml file, I want to map the operations in the xml file to the corresponding methods in the class.

I think what I am looking for is this;

@interface CalculatorActions

[Action("addOperand", "Enter first operand")]
- (void) addOperand:(double)operand1 ToOther:(double )operand2;

What is the best way?

You can create NSInvocation instances, set selectors, parameters, and optionally capture the return value. You can create all of them using strings.

< /p>

For example

SEL mySelector = NSSelectorFromString(@"testAddFunction");
Class MyClass = NSClassFromString(@"CalculatorActions");
NSString *myArgument = @"5";

NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:[MyClass instanceMethodSignatureForSelector:mySelector]];
[invocation setTarget:myClass];
[ invocation setSelector:mySelector];
[invocation setArgument:&myArgument atIndex:2];
[invocation invoke];

Note – setArgument: the selector accepts the pointer address, and the parameter index starts from 2 Start.

Leave a Comment

Your email address will not be published.