0

我今天给你们一个奇怪的,我认为我的 NSStrings 编码不正确。

NSString * convertedString = [NSString stringWithUTF8String:mesh->groupMesh[i].materialData->textureName];

-textureName 只是我要转换为 NSString 的 ac 样式字符串。

- 字符串是:“dennum1.png”

NSArray * line = [convertedString componentsSeparatedByString:@"."];

NSString * texPath = [[NSBundle mainBundle] pathForResource:line[0] ofType:line[1]];

然后我把它分成一个 NSArray 行,用句点“。”分隔。

这使得 line[0] 是 dennum1,而 line[1] 是 png。

我什至做了一个 NSLog 来确保:

NSLog(@"Name:%@ Type:%@", line[0], line[1]);

2013-09-21 02:15:27.386 SteveZissou[8846:c07] Name:dennum1 Type:png

我将其解析为 pathForResource 函数并得到(空)响应。

但是,如果我在代码 IE 中硬输入文件名:

    convertedString = @"dennum1.png";

    NSArray * line = [convertedString componentsSeparatedByString:@"."];

    NSString * texPath = [[NSBundle mainBundle] pathForResource:line[0] ofType:line[1]];

    NSLog(@"This is the texPath: %@",texPath);

有用?!

This is the texPath: /Users/meow/Library/Application Support/iPhone Simulator/6.0/Applications/2DEB8076-5F9D-45DE-8A73-10B1C8A084B4/SteveZissou.app/dennum1.png

我在代码中硬键入的 NSString 和来自转换的 NSString 的编码是否可能不同?

当我单独对它们进行 NSLog 记录时,无论类型如何,我都会得到相同的结果:

2013-09-21 02:15:27.386 SteveZissou[8846:c07] This is the c style string: dennum1.png
2013-09-21 02:15:27.386 SteveZissou[8846:c07] This is the converted c style string: dennum1.png
2013-09-21 02:15:27.386 SteveZissou[8846:c07] This is the string manually typed in: dennum1.png
4

2 回答 2

0

I figured it out after HOURS of skimming through possible code combinations and found it by accident.

I used NSURL functions to get the path based on the strings I was using, which resulted in this path:

file://localhost/Users/meow/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/2DEB8076-5F9D-45DE-8A73-10B1C8A084B4/SteveZissou.app/dennum2.png%0D

Look at the very end! That's not supposed to be there! Turns out it's called a carriage return and it was pulled with the file (probably a remnant of the files formatting), but invisible to NSLog, however it wasn't invisible to NSURL (NSURL must read the bytes and display what they are?). So snipping the carriage return off the end of the path gave me the correct file and everything is OK.

I kept thinking to myself to use a hex editor to look at the file, but couldn't find one on the mac appstore free, I think if this was windows I would've caught it in half the time.

于 2013-09-21T10:31:56.093 回答
0

如果您使用 NSPathUtilities 中旨在处理这些内容的方法会发生什么。喜欢:

NSString * texPath = [[NSBundle mainBundle] pathForResource:string.stringByDeletingPathExtension ofType:string.pathExtension];

此外,如果字符串无法正确-fileSystemRepresentation转换,它会将 NSString 转换为 a并引发异常。const char *

于 2013-09-20T16:30:18.730 回答