我正在尝试从 objective-c 向 C++ 公开 pathForResource 功能。
但是,我对 objective-c 很陌生,无法辨别如何在 objective-c 中使用 c 字符串作为参数。显然我在这里有错误的想法。
我如何让 pathForResource 使用 C 字符串作为参数?
这是我的功能:
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;
}
最佳答案
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];
关于c++ - 如何删除 "NSBundle may not respond to ' -pathForResource :ofType' "warning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1515460/