Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS isEqualToString Not Working

The following example program outputs the same, but the program does not work correctly.

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");
   }

}

Outputs:

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

does not work correctly when special characters in names. Why is this happening? Thanks ...

have the same problem here:

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

How do I make an edit here?

like image 762
user2041226 Avatar asked Dec 03 '25 16:12

user2041226


1 Answers

The documentation for isEqualToString: suggests you might have a problem:

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.

Try using (NSOrderedSame == [string1 localizedCompare:string2])

Also, if you haven't already, look into the Apple sample code 'International Mountains' which deals with numerous localization issues.

like image 68
GoZoner Avatar answered Dec 06 '25 06:12

GoZoner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!