2011-07-08 177 views
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); 
} 

的問題是,輸出變得過度條目枚舉問題

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 , , , , , , , , 

我想輸出只有

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 
+0

你期望輸出什麼? – Nico

回答

1

你的NSLog是裏面的for循環。

編輯:

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]; 
     } 
} 

看上面的代碼。你的if條件只會成功一次(我假設)..但你的其他條件會擊中所有其他不匹配。每次失敗時,都會向tempMutableArray添加一個國家對象。這是你想要的嗎?

編輯2:這應該是正確的做法。

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. 
     } 
} 
+0

是的,但問題是,我得到多餘的逗號?街道名稱,城市,新澤西州,00000,美國,,,,,, –

+0

? – Kal

+0

多餘的逗號。是的 –