1

我认为 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. 如果应该,我该如何转换或处理内部编码?
4

1 回答 1

2

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

您可以通过首先将其标准化为 C 来获得相同的长度:

NSLog(@"localPath Length:%d",[[localPath precomposedStringWithCanonicalMapping] length]);
于 2012-12-23T10:31:35.923 回答