views:

240

answers:

2

I am having this problem with the NSMutableDictionary where the values are not coming up. Snippets from my code look like this:

//Data into the Hash and then into an array

yellowPages = [[NSMutableArray alloc] init];


 NSMutableDictionary *address1=[[NSMutableDictionary alloc] init];
 [address1 setObject:@"213 Pheasant CT" forKey: @"Street"];
 [address1 setObject:@"NC" forKey: @"State"];
 [address1 setObject:@"Wilmington" forKey: @"City"];
 [address1 setObject:@"28403" forKey: @"Zip"];
 [address1 setObject:@"Residential" forKey: @"Type"];

 [yellowPages addObject:address1];


 NSMutableDictionary *address2=[[NSMutableDictionary alloc] init];
 [address1 setObject:@"812 Pheasant CT" forKey: @"Street"];
 [address1 setObject:@"NC" forKey: @"State"];
 [address1 setObject:@"Wilmington" forKey: @"City"];
 [address1 setObject:@"28403" forKey: @"Zip"];
 [address1 setObject:@"Residential" forKey: @"Type"];

 [yellowPages addObject:address2];

 //Iterate through array pulling the hash and insert into Location Object
 for(int i=0; i<locationCount; i++){
  NSMutableDictionary *anAddress=[theAddresses getYellowPageAddressByIndex:i];

  //Set Data Members
  Location *addressLocation=[[Location alloc] init];
  addressLocation.Street=[anAddress objectForKey:@"Street"];
  locations[i]=addressLocation;
  NSLog(addressLocation.Street);

 }

So the problem is only the second address is printed, the 813 and I can't figure out why. Can anyone offer any help?

+5  A: 

It is a typo. You are always assigning values to Address1 dictionary.

Laurent Etiemble
@Laurent good catch!
Jacob Relkin
......IMAO, thanks! Newbie mistake.
A: 

Laurent's answer is correct, you are assigning to an incorrect object.

Personally, I try to keep mutable objects to a minimum when I code. For example, You are creating addresses that are mutable dictionaries and storing them in a mutable array. Personally, I would use an immutable dictionary for the addresses. It would take a little more code to populate each address, but you could refactor that out to another method. The point is that rather than change an address. As a side effect, the compiler would have caught your typo because you would have been trying to change an immutable object.

Actually, it would probably even be better to create an address class rather than just populating an NSDictionary.

Abizern