IOS nsinvocation usage

Source: Internet
Author: User

 

Summary:
In iOS, you can directly call a message of an object. One of the two methods is receivmselector: withobject. The other is nsinvocation. The first method is relatively simple and can complete simple calls. But for processing two parameters or returning values, you need to do some extra work...

 

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 {

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.