2011-03-24 37 views
1

所以我有一个平台独立的API来加载一个文件,它将一个char *作为一个值。我在使用字符串文字的GetResource函数中编写了一些测试代码,它工作正常。但是,当我转换为使用char *并转换回NSString文件无法找到。将NSString转换为char时出现无法找到文件*

工作代码:

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并查看内容?

回答

6

看的NSString看到的内容,你既可以在调试器或使用查看:

NSLog(@"nsFilePath: %@", nsFilePath); 

我认为问题的至少一部分是在这条线:

NSString* nsFilePath = [NSString stringWithFormat:@"%c" , filePath]; 

%c是单个8位无符号字符的格式说明符。对于C风格的字符串,您可能希望使用%s,它是8位无符号字符的以空字符结尾的数组的格式说明符。

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