NSFileManager contentOfDirectoryAtPath 与 samba 路径的编码问题
我使用此代码安装 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您出于某种原因特别需要 MacRoman,否则您可能应该在此处使用 UTF-8。
您期望组合字符并获得分解序列。
由于您是从文件系统获取路径名,所以这不是问题:当您接收路径名时,路径名是正确的,并且只要您将它们传递给正确执行 Unicode 的东西,它们也会正确显示。
Unless you specifically need MacRoman for some reason, you should probably be using UTF-8 here.
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.
好吧,四年后,我正在为同样的事情而苦苦挣扎,但就我而言,除了 åäö。
花了很多时间才找到简单的解决方案。
NSString 内置了必要的比较器。
将
aString
与anotherString
进行比较,其中一个来自 NSFileManagerscontentsOfDirectoryAtPath:
返回的数组,非常简单:< 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
withanotherString
where one comes from the array returned by NSFileManagerscontentsOfDirectoryAtPath:
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"