NSFileManager contentOfDirectoryAtPath 与 samba 路径的编码问题

发布于 2024-09-30 21:39:17 字数 935 浏览 5 评论 0原文

我使用此代码安装 SMB 路径

urlStringOfVolumeToMount = [urlStringOfVolumeToMount stringByAddingPercentEscapesUsingEncoding:NSMacOSRomanStringEncoding];
NSURL *urlOfVolumeToMount = [NSURL URLWithString:urlStringOfVolumeToMount];
FSVolumeRefNum returnRefNum;
FSMountServerVolumeSync( (CFURLRef)urlOfVolumeToMount, NULL, NULL, NULL, &returnRefNum, 0L);

然后,我获取某些路径的内容:

NSMutableArray *content = (NSMutableArray *)[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];

我的问题是“内容”数组中包含特殊字符(例如 ü)的每个路径给我 2 个编码的字符:ü 变为

u¡我使用以下方式记录字节:

[contentItem dataUsingEncoding:NSUTF8StringEncoding];

它给了我:75cc88,即 u (75) 和 ¡(cc88)

我期望的是用 utf-8 编码的 ü 字符。以字节为单位,应该是 c3bc

我尝试使用 ISOLatin1 编码、MacOSRoman 转换我的路径...但只要内容路径已经有 2 个单独的字符而不是 ü 的一个,任何转换都会给我编码 2 个字符。如果

有人可以提供帮助,谢谢

我的配置:本地化为法语并使用雪豹。

i mount a SMB path using this code

urlStringOfVolumeToMount = [urlStringOfVolumeToMount stringByAddingPercentEscapesUsingEncoding:NSMacOSRomanStringEncoding];
NSURL *urlOfVolumeToMount = [NSURL URLWithString:urlStringOfVolumeToMount];
FSVolumeRefNum returnRefNum;
FSMountServerVolumeSync( (CFURLRef)urlOfVolumeToMount, NULL, NULL, NULL, &returnRefNum, 0L);

Then, i get the content of some paths :

NSMutableArray *content = (NSMutableArray *)[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];

My problem is every path in "content" array containing special chars (ü for example) give me 2 chars encoded : ü becomes u¨

when i log bytes using :

[contentItem dataUsingEncoding:NSUTF8StringEncoding];

it gives me : 75cc88 which is u (75) and ¨(cc88)

What i expected is the ü char encoded in utf-8. In bytes, it should be c3bc

I've tried to convert my path using ISOLatin1 encoding, MacOSRoman... but as long as the content path already have 2 separate chars instead of one for ü, any conversion give me 2 chars encoded...

If someone can help, thanks

My configuration : localized in french and using snow leopard.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巷雨优美回忆 2024-10-07 21:39:17
urlStringOfVolumeToMount = [urlStringOfVolumeToMount stringByAddingPercentEscapesUsingEncoding:NSMacOSRomanStringEncoding];

除非您出于某种原因特别需要 MacRoman,否则您可能应该在此处使用 UTF-8。

NSMutableArray *content = (NSMutableArray *)[[NSFileManager defaultManager]contentsOfDirectoryAtPath:路径错误:&错误];

我的问题是“content”数组中包含特殊字符(例如ü)的每个路径给我2个编码字符:ü变成uì

您期望组合字符并获得分解序列。

由于您是从文件系统获取路径名,所以这不是问题:当您接收路径名时,路径名是正确的,并且只要您将它们传递给正确执行 Unicode 的东西,它们也会正确显示。

urlStringOfVolumeToMount = [urlStringOfVolumeToMount stringByAddingPercentEscapesUsingEncoding:NSMacOSRomanStringEncoding];

Unless you specifically need MacRoman for some reason, you should probably be using UTF-8 here.

NSMutableArray *content = (NSMutableArray *)[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];

My problem is every path in "content" array containing special chars (ü for example) give me 2 chars encoded : ü becomes u¨

You're expecting composed characters and getting decomposed sequences.

Since you're getting the pathnames from the file-system, this is not a problem: The pathnames are correct as you're receiving them, and as long as you pass them to something that does Unicode right, they will display correctly as well.

世界等同你 2024-10-07 21:39:17

好吧,四年后,我正在为同样的事情而苦苦挣扎,但就我而言,除了 åäö。
花了很多时间才找到简单的解决方案。

NSString 内置了必要的比较器。

aStringanotherString 进行比较,其中一个来自 NSFileManagers contentsOfDirectoryAtPath: 返回的数组,非常简单:< br>
if( [aStringcompare:anotherString] == NSOrderedSame )

compare 方法负责将两个字符串转换为可比较的规范格式。实际上使它们“如果它们看起来相同,那么它们就是相同的”

Well, four years later I'm struggling with the same thing but for åäö in my case.
Took a lot of time to find the simple solution.

NSString has the necessary comparator built in.

Comparing aString with anotherString where one comes from the array returned by NSFileManagers contentsOfDirectoryAtPath: is as simple as:
if( [aString compare:anotherString] == NSOrderedSame )

The compare method takes care of making both the strings into a comparable canonical format. In effect making them "if they look the same, they are the same"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文