Tur*_*age 1 iphone objective-c
所以我有一个独立于平台的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"];
Run Code Online (Sandbox Code Playgroud)
不工作代码:
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"];
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我错了什么?有没有一种简单的方法来查看NSString并查看内容?
要查看NSString以查看内容,您可以在调试器中查看它或使用:
NSLog(@"nsFilePath: %@", nsFilePath);
Run Code Online (Sandbox Code Playgroud)
我认为至少部分问题出在这一行:
NSString* nsFilePath = [NSString stringWithFormat:@"%c" , filePath];
Run Code Online (Sandbox Code Playgroud)
%c是单个8位无符号字符的格式说明符.对于C样式字符串,您可能希望使用%s,这是8位无符号字符的以null结尾的数组的格式说明符.
此外,从C字符串转换为NSString的更直接的方法是使用+ (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)encNSString类的方法.有关更多详细信息,请参阅NSString类参考.
| 归档时间: |
|
| 查看次数: |
354 次 |
| 最近记录: |