Ios4 枚举问题
问题是输出的条目过多Ios4 枚举问题,ios4,nsmutablearray,nsarray,enumeration,abaddressbook,Ios4,Nsmutablearray,Nsarray,Enumeration,Abaddressbook,问题是输出的条目过多 NSMutableArray *tempMutableArray = [[NSMutableArray alloc] init]; if (street != NULL) { [tempMutableArray addObject:(NSString *)street]; } if (city != NULL) { [tempMutableArray addObject:(NSString *)city]; } if (state != NULL) { [tempMutab
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 , , , , , , , ,
您的NSLog在for循环中 编辑:
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
看看上面的代码。你的if条件只会成功一次(我假设)。。但是你的else条件将对所有其他非比赛产生影响。每次失败时,您都会向tempMutableArray添加一个country对象。这是你想要的吗
编辑2:这应该是正确的方法
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];
}
}
是的,但问题是我得到了过多的街道名,纽约市,00000,美国,,,我想我看到了问题所在。添加country元素的else部分也位于for循环中,该循环在country数组中迭代。我认为您希望在外部执行此操作。country元素用于确定联系人是否有美国地址(或加拿大地址)。有几种变体或指示您居住在美国(美国/美国/等)。代码是如何创建所有额外的逗号的?谢谢Kal。我读了你上面编辑的帖子。如果匹配,则创建一个空字符串,否则使用应存储在元素中的字符串。@Cocoa-Dev--没错。仅在匹配时添加到tempMutableArray。
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.
}
}