iphone - IOS isEqualToString 不工作

标签 iphone ios objective-c nsstring nsfilemanager

下面的示例程序输出相同,但程序无法正常运行。

NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager]
                                   enumeratorAtPath:kDocdir];

for (NSString *pathi in directoryEnumerator)
{
   NSString *fileName_Manager = [pathi lastPathComponent];
   NSLog(@"fileName_Manager = %@",fileName_Manager);

   Artist *name_Databse =  [self.fetchedResultsController
                               objectAtIndexPath:IndexPath];
   NSLog(@"name_Databse     = %@",name_Databse.name);



   if ([fileName_Manager isEqualToString:name_Databse.name]) {
       NSLog(@"Same Name");
   }else{
       NSLog(@"Different Name");
   }

}

输出:

2013-04-25 15:37:43.256 Player[36436:907] fileName_Manager = alizée - mèxico - final j'en
2013-04-25 15:37:43.272 Player[36436:907] name_Databse     = alizée - mèxico - final j'en
2013-04-25 15:37:44.107 Player[36436:907] Different Name

名称中有特殊字符时无法正常工作。为什么会这样? 谢谢...

这里有同样的问题:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@",[pathi lastPathComponent]];

如何在此处进行编辑?

最佳答案

isEqualToString: 的文档表明您可能遇到了问题:

The comparison uses the canonical representation of strings, which for a particular string is the length of the string plus the Unicode characters that make up the string. When this method compares two strings, if the individual Unicodes are the same, then the strings are equal, regardless of the backing store. “Literal” when applied to string comparison means that various Unicode decomposition rules are not applied and Unicode characters are individually compared. So, for instance, “Ö” represented as the composed character sequence “O” and umlaut would not compare equal to “Ö” represented as one Unicode character.

尝试使用 (NSOrderedSame == [string1 localizedCompare:string2])

此外,如果您还没有,请查看 Apple 示例代码“International Mountains”,它处理了许多本地化问题。

关于iphone - IOS isEqualToString 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16215439/

相关文章:

iphone - 是否可以有选择地覆盖 Interface Builder 中的布局约束?

iphone - AlertView 应该自行消失

iphone - 未调用 glkView drawInRect

ios - 打开远程通知时,带有标识符的应用程序 ID 不可用

ios - Parse.com 推送通知角标(Badge)值

ios - 将数据从一个 View 传递到另一个 View (Storyboard,iOS)

ios - 如果我的应用程序要长期使用,我应该将我的 ObjC 应用程序转换为 Swift 吗?

ios - UIImagepicker 显示图像捕获而不是视频录制

ios - 无法保存 AFNetworking responseObject

ios - 如何有条件地包含在 XCode 中的 "Compiled Sources"