Reputation: 134
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
Reputation: 27235
Sample Code :
-(BOOL) testMethod :(NSString *)largestring :(NSString *)smallstring {
return YES;
}
Explanation : Check Apple Documentation.
Use :
[self testMethod :yourLargeString :yourSmallString];
And do I have to include some library so I can use string type ?
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
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:
.mm
. The extension tells the compiler that the file contains mixed C++ and Objective-C codestd::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.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)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 :-)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
Reputation: 1189
Well here are multiple ways:
extern "C"
..mm
extension (that is, Objective-C++) and call C++ directly.There is some limitations:
-(void) dealloc
.And a good point:
Upvotes: 3
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