Objective-C calls C ++ code

Source: Internet
Author: User

I want to call C ++'sCodeHere, I will summarize my methods,The Hello class is only an encapsulation of Calling C ++ for objective-C..

However, I feel that this is too inconvenient. If there are a lot of C ++ code, it will be very difficult to do so. I hope someone will provide a good solution,ArticleThe demoSource code.
Reference: http://blog.csdn.net/zhouhuiah/article/details/6426158

Write down the demo content
1. Create a new project. I chose "single view application ".
2. Create an objective-C class file named hello
3. There will be two files in the project: Hello. h and hello. M. Change the suffix of the hello. M file to. Mm, that is, hello. Mm.
4. Add the C ++ class to the hello. h file. The content of the hello. h file is as follows:

 # Import <Foundation/Foundation. h> @ Interface  Hello: nsobject {  Class Newhello {  Private : Int  Greeting_text;  Public  : Newhello () {greeting_text = 5  ;}  Void  Say_hello () {printf (  "  Greeting_text = % d  "  , Greeting_text) ;}}; newhello *Hello ;} -( Void  ) Sayhellooooo;  @ End 

5. Implement the sayhellooooo method in the hello. MM file. Call the C ++ class in this method.

 
# Import "Hello. h"@ ImplementationHello-(Void) Sayhellooooo {hello=NewNewhello (); Hello->Say_hello ();}@ End

6. Use @ Class hello instead of # import or # include to call the C ++ class.

# Import<Uikit/uikit. h>//# Include "Hello. H"@ ClassHello;@ InterfaceViewcontroller: uiviewcontroller {}-(Void) AAAA;@ End

7. Call the methods in the C ++ class

 # Import   "  Viewcontroller. h  "  @ Interface  Viewcontroller ()  @ End @ Implementation  Viewcontroller -( Void  ) Viewdidload {[Super viewdidload]; nslog (  @"  Dddddddd  "  ); Hello * AA = [[Hello alloc] init]; [AA sayhellooooo];  //  Do any additional setup after loading the view, typically from a nib.  } -( Void ) Viewdidunload {[Super viewdidunload];  //  Release any retained subviews of the main view.  } - (Bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation {  Return (Interfaceorientation! = Uiinterfaceorientationportraitupsidedown );} -( Void  ) AAAA {}  @ End 

8. Source Code:Objective-C calls C ++Blog park requires that the file name cannot have a plus sign

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.