Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 将NSString转换为char时找不到文件*_Iphone_Objective C - Fatal编程技术网

Iphone 将NSString转换为char时找不到文件*

Iphone 将NSString转换为char时找不到文件*,iphone,objective-c,Iphone,Objective C,所以我有一个独立于平台的API加载一个文件,它以char*作为值。我在GetResource函数中编写了一些测试代码,使用了字符串文字,效果很好。但是,当我转换为使用char*并转换回NSString时,找不到该文件 工作代码: bool GetResource(const char* filePath, uint8*& allocatedBuffer, uint32& size) { bool success = false; NSString* path =

所以我有一个独立于平台的API加载一个文件,它以char*作为值。我在GetResource函数中编写了一些测试代码,使用了字符串文字,效果很好。但是,当我转换为使用char*并转换回NSString时,找不到该文件

工作代码:

bool GetResource(const char* filePath, uint8*& allocatedBuffer, uint32& size) {

    bool success = false;
    NSString* path = [[NSBundle mainBundle] pathForResource: "models/testModel" ofType: @"obj"];
NSString* nsModelString = @"models/testModel";
gDatastore->GetResource([nsModelString UTF8String], buffer, size);

....

bool GetResource(const char* filePath, uint8*& allocatedBuffer, uint32& size) {

    bool success = false;
    NSString* nsFilePath = [NSString stringWithFormat:@"%c" , filePath];
    NSString* path = [[NSBundle mainBundle] pathForResource: nsFilePath ofType: @"obj"];
非工作代码:

bool GetResource(const char* filePath, uint8*& allocatedBuffer, uint32& size) {

    bool success = false;
    NSString* path = [[NSBundle mainBundle] pathForResource: "models/testModel" ofType: @"obj"];
NSString* nsModelString = @"models/testModel";
gDatastore->GetResource([nsModelString UTF8String], buffer, size);

....

bool GetResource(const char* filePath, uint8*& allocatedBuffer, uint32& size) {

    bool success = false;
    NSString* nsFilePath = [NSString stringWithFormat:@"%c" , filePath];
    NSString* path = [[NSBundle mainBundle] pathForResource: nsFilePath ofType: @"obj"];

谁能告诉我我用错了什么?有没有一种简单的方法可以查看NSString并查看其内容?

要查看NSString以查看其内容,您可以在调试器中查看它,也可以使用:

NSLog(@"nsFilePath: %@", nsFilePath);
我认为至少有一部分问题在这方面:

NSString* nsFilePath = [NSString stringWithFormat:@"%c" , filePath];
%c
是单个8位无符号字符的格式说明符。对于C样式的字符串,您可能希望使用
%s
,它是以空结尾的8位无符号字符数组的格式说明符


另外,从C字符串转换为NSString的更直接的方法是使用NSString类的
+(id)stringWithCString:(const char*)cString编码:(NSStringEncoding)enc
方法。有关更多详细信息,请参阅。

要查看NSString以查看其内容,您可以在调试器中查看它,也可以使用:

NSLog(@"nsFilePath: %@", nsFilePath);
我认为至少有一部分问题在这方面:

NSString* nsFilePath = [NSString stringWithFormat:@"%c" , filePath];
%c
是单个8位无符号字符的格式说明符。对于C样式的字符串,您可能希望使用
%s
,它是以空结尾的8位无符号字符数组的格式说明符

另外,从C字符串转换为NSString的更直接的方法是使用NSString类的
+(id)stringWithCString:(const char*)cString编码:(NSStringEncoding)enc
方法。有关更多详细信息,请参阅