I have two strings that are exactly the same, but they are not being compared as equal. One string is a mutable string, and the other is a normal string, and I have a if statement that checks if the two strings are equal. The output for the two strings is this:
2013-04-08 09:09:31.555 Pin2Own[650:1303] 6447 E Crocus Dr
2013-04-08 09:09:31.555 Pin2Own[650:1303] 6447 E Crocus Dr
The top is a mutable string, and the bottom is the normal string. I have tried converting the mutable string to a normal string, but I am having the same problem. I am getting the mutable string from a xml document in a NSXMLParser subclass, and the other from a mutable array in a singleton. Here is the code for the NSXMLParser class:
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
address = nil;
zpid = nil;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"street"])
address = [[NSMutableString alloc] init];
if ([elementName isEqualToString:@"zpid"])
zpid = [[NSMutableString alloc] init];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(address)
[address setString:string];
else if(zpid)
[zpid setString:string];
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"street"])
NSLog(@"%@", address);
if ([elementName isEqualToString:@"zpid"])
NSLog(@"zip id is: %@", zpid);
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSMutableArray *annotations = [[Data singleton] annotations];
for (MapPoint *mp in annotations)
{
NSLog(@"%@", mp.fullAddress);
if ([mp.fullAddress isEqualToString:address])
{
mp.zpid = zpid;
}
else
{
NSLog(@"Did not match address");
}
}
}
SO I am comparing the two addresses, and setting a property if the addresses are the same, which I have made sure will always be true. Does anyone see if there is something wrong? I really have no idea why this is happening.
try trimming the whitespace characters from your strings before comparing them. You can use the following
firstString = [firstString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
secondString = [secondString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
BOOL stringsAreEqual = [firstString isEqualToString:secondString];