0
NSMutableArray *tempMutableArray = [[NSMutableArray alloc] init];
if (street != NULL) {
[tempMutableArray addObject:(NSString *)street];
}
if (city != NULL) {
[tempMutableArray addObject:(NSString *)city];
}
if (state != NULL) {
[tempMutableArray addObject:(NSString *)state];
}                    
if (zip != NULL) {
[tempMutableArray addObject:(NSString *)zip];
}
if (country != NULL) {
// Check to see if the country is USA/Canada
NSStringCompareOptions  compareOptions = NSDiacriticInsensitiveSearch;
NSArray* countryIndex = [[NSArray alloc] initWithObjects:@"United States of America", @"United States", @"U.S.A.", @"USA", @"US", @"U.S.", @"Canada", @"CAN", @"CDN", @"CA", nil];

for (NSString* element in countryIndex) 
{
NSComparisonResult result = [(NSString *)country compare:element options:compareOptions];
if (NSOrderedSame == result) {
// Do another thing here if they match...
CFRelease(country);
country = CFStringCreateWithCString(NULL, "", kCFStringEncodingUTF8);
}
else {
// Try something else...
[tempMutableArray addObject:(NSString *)country];
}
}
}
for (NSString* element in tempMutableArray) 
{
    syntheticAddress = [syntheticAddress stringByAppendingString:[NSString stringWithFormat:@"%@ ,", element]];
NSLog(@"synthetic address is %@", syntheticAddress);
}

The problem is that the the output is getting excessive entries

2011-07-08 14:42:38.077 TestingApp[3770:ef03] synthetic address is 123 Main Street ,
2011-07-08 14:42:40.673 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,
2011-07-08 14:42:42.510 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,
2011-07-08 14:42:44.136 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,00000 ,
2011-07-08 14:42:45.637 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,00000 ,United States ,
2011-07-08 14:42:49.968 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,00000 ,United States , ,
2011-07-08 14:42:52.046 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,00000 ,United States , , ,
2011-07-08 14:42:54.306 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,00000 ,United States , , , ,
2011-07-08 14:42:55.730 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,00000 ,United States , , , , ,
2011-07-08 14:42:58.487 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,00000 ,United States , , , , , ,
2011-07-08 14:43:00.035 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,00000 ,United States , , , , , , ,
2011-07-08 14:43:01.237 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,00000 ,United States , , , , , , , ,
2011-07-08 14:43:06.263 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,00000 ,United States , , , , , , , , ,
2011-07-08 14:43:10.537 TestingApp[3770:ef03] Address is 123 Main Street ,SomeCity ,AA ,00000 ,United States , , , , , , , ,

I would like the output to only be

2011-07-08 14:42:38.077 TestingApp[3770:ef03] synthetic address is 123 Main Street ,
2011-07-08 14:42:40.673 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,
2011-07-08 14:42:42.510 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,
2011-07-08 14:42:44.136 TestingApp[3770:ef03] synthetic address is 123 Main Street ,SomeCity ,AA ,00000
Cocoa Dev
  • 9,361
  • 31
  • 109
  • 177

1 Answers1

1

Your NSLog is inside the for loop.

EDIT :

for (NSString* element in countryIndex) 
{
      NSComparisonResult result = [(NSString *)country compare:element options:compareOptions];
      if (NSOrderedSame == result) {
              // Do another thing here if they match...
              CFRelease(country);
              country = CFStringCreateWithCString(NULL, "", kCFStringEncodingUTF8);
       }
       else {
          // Try something else...
          [tempMutableArray addObject:(NSString *)country];
       }
 }

Look at the code above. Your if condition will only succeed once (I'm assuming ) .. but your else condition will hit for all the other non-matches. And each time it fails, you are adding a country object to your tempMutableArray. Is this what you want?

EDIT 2: This should be the correct way of doing it.

for (NSString* element in countryIndex) 
{
      NSComparisonResult result = [(NSString *)country compare:element options:compareOptions];
      if (NSOrderedSame == result) {
          [tempMutableArray addObject:(NSString *)country];
          break;
       }
       else {
          // Try something else...
          // No match. Go on to the next element compare.
       }
 }
Kal
  • 24,724
  • 7
  • 65
  • 65
  • Yes it is but the issue is that I'm getting excess StreetName ,City ,NJ ,00000 ,United States , , , , , , , , – Cocoa Dev Jul 08 '11 at 18:51
  • I think I see the problem. Your else part where you add the country element is also inside the for loop that iterates through the country array. I think you want to do that outside. – Kal Jul 08 '11 at 18:56
  • The country element is used to determine if the contact has a US address (or canadian). There are several variations or indicating that you live in the US (USA/U.S.A/etc). How is that code creating all the extra comma's? – Cocoa Dev Jul 08 '11 at 20:08
  • Thanks Kal. I read your edited post above. If its a Match then create an empty string else use the string that should be stored in element. – Cocoa Dev Jul 12 '11 at 12:56
  • @Cocoa Dev -- That's right. Add to your tempMutableArray ONLY on match. – Kal Jul 12 '11 at 14:19
  • If its not a match (i.e. another country) then I want to store it. So what do we do on the else match? Should I break the loop if it is a match? – Cocoa Dev Jul 12 '11 at 14:45