user2331875
user2331875

Reputation: 134

How to call c++ method inside objective C

I made some method in pseudo code, and I want to create objective C method. Could you help me define it? I am not very familiar with objective C, so I am confused. Also, I want to use strings as parameters for the method, but not NSString objects.

this is the method definition. I am not sure if it fits objective C definitions and also I am unsure about the type "string".

-(BOOL) testMethod(string largestring, string smallstring) {
    return true;
}

And do I have to include some library so I can use string type? Thanks.

Upvotes: 0

Views: 1306

Answers (4)

Bhavin
Bhavin

Reputation: 27235

Sample Code :

-(BOOL) testMethod :(NSString *)largestring :(NSString *)smallstring {
    return YES;
}

Explanation : Check Apple Documentation.

enter image description here

Use : [self testMethod :yourLargeString :yourSmallString];


And do I have to include some library so I can use string type ?

  • Answer : No. Actually, NSString.h is declared inside Foundation.framework and Xcode by default adds this framework to your Project when you creates new one (Check inside Frameworks folder). So, you have implicitly added that Library and that's why no need to include specific NSString class.

Upvotes: 1

herzbube
herzbube

Reputation: 13388

You said you want to use strings as parameters, so I assume you don't want C-style char* but some sort of string class. Since you didn't specify anything else, in my example I am using std::string from the C++ standard library.

This is how your method looks like in Objective-C syntax:

#import <string>

- (BOOL) testMethodWithLargeString:(const std::string&)largeString smallString:(const std::string&)smallString
{
  return YES;
}

Some notes:

  • To make this work you must place this in a file with the extension .mm. The extension tells the compiler that the file contains mixed C++ and Objective-C code
  • std::string comes from the C++ standard library, but I believe you do not have to do anything special to make your project link against that library.
  • You must decide whether you want to use BOOL (uppercase) or bool (lowercase) as your return type. BOOL is the traditional Objective-C boolean type, whereas bool is the traditional C++ primitive boolean type. With BOOL you must use YES and NO as return values, whereas with bool you must use true and false. You should not try to mix the two types and their values, it will not make the compiler happy (and if you use casts to force the compiler to accept the mix, sooner or later you will not be happy)
  • Try to stick to const std::string& as the type for your in parameters. The const will save you from accidentally changing the content of your parameters. If you use just std::string& (without the const) the parameter becomes an out parameter. I also recommend you do not use pointers (i.e. const std::string* or std::string*) because std::string is a value type (read up on that topic if it is unfamiliar) and thus is usually combined with references. Last but not least: Never ever use std::string (i.e. without & [reference] or * [pointer]) - if you do, a copy of the string will be created each time you invoke the method. If you already know about all these things, then of course you can ignore my advice :-)
  • A final word in regard to the naming of the method: It is a peculiarity of Objective-C that method names are split up in "sections", one for each parameter. This has the advantage that you can build parameter names right into the method name, with the result that when you invoke methods the code is much more expressive and clearer to read. A similar feature in other languages are named parameters (aka keyword arguments).

The answer by Maxthon Chan also has useful hints about what you need to take into account when you mix C++ and Objective-C.

Upvotes: 1

Maxthon Chan
Maxthon Chan

Reputation: 1189

Well here are multiple ways:

  1. In your C++ code, provide wrapper functions of your C++ methods in C, with extern "C".
  2. Name your Objective-C code with .mm extension (that is, Objective-C++) and call C++ directly.

There is some limitations:

  • Objective-C cannot catch C++ exceptions and in earlier versions C++ cannot catch Objective-C exceptions too.
  • In earlier versions, C++ destructors are not called when an object is released. You need to manually do that in -(void) dealloc.
  • You cannot put C++ objects into Objective-C containers.

And a good point:

  • In Objective-C++ code, you can mix C++11 lambda expression with Objective-C blocks - with clang 3.3 they are interchangeable.

Upvotes: 3

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

Your method should be like this

-(BOOL) testMethodWithLargeString:(NSString *)largestring smallString(NSString *)smallstring {
    return YES;
}

For accessing this method :

BOOL result = [<classObject> testMethodWithLargeString:string1 smallString:string2];

If the method is in self class, use self otherwise you need to use an object or class.

Also, a convention of writing multiple parameterized method, (breaking it into multiple line) :

BOOL result = [<classObject> testMethodWithLargeString:string1 
                                           smallString:string2];

And do I have to include some library so I can use string type?

NO, You dont have to use any library like string.h

In cocoa Foundation framework all these are included.

Upvotes: 1

Related Questions