【问题标题】:Weird bug of Multi-language NSString. Maybe issue of Encoding多语言 NSString 的奇怪错误。也许是编码问题
【发布时间】:2012-12-23 00:28:19
【问题描述】:

我认为 NSString 在多语言支持方面存在奇怪的错误。

我正在使用 iOS SDK 6.0 进行开发,并且在模拟器和 iPhone 上都存在同样的问题。

这是我的代码。

 NSString* localPath = [documentsPath stringByAppendingPathComponent:filename];
 NSLog(@"%@",localPath);
 NSLog(@"%@",@"/Users/vicjames/Library/Application Support/iPhone Simulator/6.0/Applications/8186A14C-1482-44AB-AD3A-0A88DC40EC89/Documents/Data/해부학/11월 29일/095. Michael Learns to Rock - 25 Minutes.mp3");

 NSLog(@"localPath Length:%d",[localPath length]);
 NSLog(@"String Length:%d",[@"/Users/vicjames/Library/Application Support/iPhone Simulator/6.0/Applications/8186A14C-1482-44AB-AD3A-0A88DC40EC89/Documents/Data/해부학/11월 29일/095. Michael Learns to Rock - 25 Minutes.mp3" length]);


 NSLog(@"localPath : URL: %@",[[NSURL fileURLWithPath:localPath] absoluteString]);
 NSLog(@"String URL: %@",[[NSURL fileURLWithPath:@"/Users/vicjames/Library/Application Support/iPhone Simulator/6.0/Applications/8186A14C-1482-44AB-AD3A-0A88DC40EC89/Documents/Data/해부학/11월 29일/095. Michael Learns to Rock - 25 Minutes.mp3"] absoluteString]);

下面的日志是代码的执行结果。

2012-12-23 00:11:57.741 AudioArchive[11702:c07] /Users/vicjames/Library/Application Support/iPhone Simulator/6.0/Applications/8186A14C-1482-44AB-AD3A-0A88DC40EC89/Documents/Data/해부학/11월 29일/095. Michael Learns to Rock - 25 Minutes.mp3
2012-12-23 00:11:57.741 AudioArchive[11702:c07] /Users/vicjames/Library/Application Support/iPhone Simulator/6.0/Applications/8186A14C-1482-44AB-AD3A-0A88DC40EC89/Documents/Data/해부학/11월 29일/095. Michael Learns to Rock - 25 Minutes.mp3

两个字符串看起来一样。

2012-12-23 00:11:57.742 AudioArchive[11702:c07] localPath Length:194
2012-12-23 00:11:57.742 AudioArchive[11702:c07] String Length:186

但长度不同。

2012-12-23 00:11:57.743 AudioArchive[11702:c07] localPath : URL: file://localhost/Users/vicjames/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/8186A14C-1482-44AB-AD3A-0A88DC40EC89/Documents/Data/%E1%84%92%E1%85%A2%E1%84%87%E1%85%AE%E1%84%92%E1%85%A1%E1%86%A8/11%E1%84%8B%E1%85%AF%E1%86%AF%2029%E1%84%8B%E1%85%B5%E1%86%AF/095.%20Michael%20Learns%20to%20Rock%20-%2025%20Minutes.mp3
2012-12-23 00:11:57.743 AudioArchive[11702:c07] String URL: file://localhost/Users/vicjames/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/8186A14C-1482-44AB-AD3A-0A88DC40EC89/Documents/Data/%ED%95%B4%EB%B6%80%ED%95%99/11%EC%9B%94%2029%EC%9D%BC/095.%20Michael%20Learns%20to%20Rock%20-%2025%20Minutes.mp3

URL 编码显示了差异。

2012-12-23 00:11:57.745 AudioArchive[11702:c07] is Not Equal

NSString 也表示两个字符串不相等。

  1. 我想知道为什么会发生这种情况。
  2. 我应该考虑内部 NSString 编码吗?
  3. 如果需要,如何转换或处理内部编码?

【问题讨论】:

    标签: ios encoding ios6 nsstring


    【解决方案1】:

    iOS 文件系统名称在 normalization form D 中,而您的字符串文字采用规范化形式 C。

    你可以通过先将其归一化为 C 来获得相同的长度:

    NSLog(@"localPath Length:%d",[[localPath precomposedStringWithCanonicalMapping] length]);
    

    【讨论】:

    • 谢谢!!正如您所回答的,这段代码解决了所有问题~ " localPath = [localPath precomposedStringWithCanonicalMapping]; "
    猜你喜欢
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多