views:

390

answers:

1

I am trying to expose the pathForResource functionality to C++ from objective-c.
However, I am very new to objective-c and have not been able to discern how to use a c string as an argument in objective-c. clearly I have the wrong idea here. how do i get pathForResource to use c strings as an argument?

here is my function:

    static std::string getAssetPath(std::string name, std::string ending)
{

 const char * nameChar = name.c_str();
 const char * endingChar = ending.c_str();
 NSString* assetPath = [NSBundle pathForResource:&nameChar
            ofType:&endingChar];
 std::string str;
 str = std::string([assetPath cStringUsingEncoding: NSASCIIStringEncoding]);
 return str;
}
+3  A: 
NSString* nameChar = [NSString stringWithCString:name.c_str() encoding:NSUTF8StringEncoding];    
NSString* endingChar = [NSString stringWithCString:ending.c_str() encoding:NSUTF8StringEncoding];

NSString* assetPath = [[NSBundle mainBundle] pathForResource:nameChar ofType:endingChar];
Darren
this still leaves the warning for me though. I could be missing something? ignore warning?
Jorge
You cannot ignore that warning. It means the method was not found; it's an error in any other language. pathForResource:ofType is an instance method, so you need to call it on an instance of NSBundle, such as the "main bundle". I've fixed code in my example.
Darren
wow thanks so much! That gives me a good idea of what was going on.
Jorge