I have an NSMutableArray where each item is an NSMutableDictionary.
NSMutableAray *services = [NSMutableArray new];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject: aNetService forKey: @"net_service"];
[dict setObject: [aNetService name] forKey: @"net_service_name"];
[self.services addObject:dict];
Then I want to retrieve an item according to the “net_service_name” key. So, I tried the following:
-(void)netServiceBrowser:(NSNetServiceBrowser *)aBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)more {
NSLog(@"netservname%@",[aNetService name]);
for (int i = 0; i < [services count]; i++)
{
NSDictionary *dict = [services objectAtIndex:i];
NSLog(@"netservname%@",[dict objectForKey:@"net_service_name"]);
if ([NSString stringWithFormat:@"%@",[dict objectForKey:@"net_service_name"]] == [NSString stringWithFormat:@"%@",[aNetService name]]){
NSLog(@"Match");
}
}
}
In the console both NSLog(@”netservname”) are the same, but I’m not getting the “Match” message. Can anyone see why? Thanks very much!
Try using
if ([[dict objectForKey:@"net_service_name"] isEqualToString:[aNetService name]]).==checks for identity, that is whether the two objects point to the same memory address.isEqualToStringchecks for equality, in this case, that the two strings are the same characters in the same order.