枚举问题

发布于 2024-11-18 22:22:55 字数 3452 浏览 5 评论 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
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白芷 2024-11-25 22:22:55

您的 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 条件只会成功一次(我假设)..但是你的 else 条件将满足所有其他不匹配的情况。每次失败时,您都会向 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.
       }
 }

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.
       }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文