In iOS, two message methods can be used to directly call an object:
One is javasmselector: withobject:
The other is nsinvocation.
The first method is relatively simple and can complete simple calls. However, if you want to process more than two parameters or have returned values, you need to do some additional work. In this case, we can use nsinvocation to perform these relatively complex operations. |
Main. h [HTML]
View plaincopy
1. # Import <Foundation/Foundation. h> 2. # import "myclass. H"
3. 4. Int main (INT argc, const char * argv [])
5 .{
6. 7. nsautoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
8. 9. myclass * myclass = [[myclass alloc] init];
10. nsstring * mystring = @ "my string ";
11. 12. // common call 13. nsstring * normalinvokestring = [myclass appendmystring: mystring];
14. nslog (@ "the normal invoke string is: % @", normalinvokestring );
15. 16. // nsinvocation call 17. Sel Myselector = @ selector (appendmystring :);
18. nsmethodsignature * Sig = [[myclass class]
19. instancemethodsignatureforselector: myselector];
20. 21. nsinvocation * Myinvocation = [nsinvocation invocationwithmethodsignature: SIG];
22. [myinvocation settarget: myclass];
23. [myinvocation setselector: myselector];
24. 25. [myinvocation setargument: & mystring atindex: 2];
26. 27. nsstring * Result = nil;
28. [myinvocation retainarguments];
29. [myinvocation invoke];
30. [myinvocation getreturnvalue: & Result];
31. nslog (@ "The nsinvocation invoke string is: % @", result );
32. 33. [myclass release];
34. 35. [pool drain];
36. Return 0;
37 .}
Myclass. h
[HTML]
View plaincopy
1. # Import <Foundation/Foundation. h> 2. 3. 4. @ interface myclass: nsobject {
|