0

我在数据库中保留了一个字符串字段,该字段应该是唯一的。因此,每当我想使用 Text 字段为该字段添加新记录时,我都必须测试这样的记录是否已经存在。如果我已经添加了“A”,那么以后我什至不能添加“a”。但是使用我的代码,如果我添加'A',则不会添加'A',而是添加'a'。那么这个“案例”问题的解决方案是什么。我也试过 [someString compare:otherString options:NSCaseInsensitiveSearch] 但我没有为我工作。

这是我的代码:

isDuplicate = FALSE;

            for(int i=0; i<[streetArray count]; i++) // This array contains my field
            {
                strToCompare = (NSString *)[[streetArray objectAtIndex:i]Address];
                if ([[textfield1.text lowercaseString] isEqualToString:[strToCompare lowercaseString]]) // Here I compare stings by making them lower
                {
                    isDuplicate = TRUE;
                    break;
                }
            }

            if(isDuplicate == TRUE)
            {
                [self showAlert:nil withMessage:@"Data already exists"];
            }
4

2 回答 2

2

如此处所述您可以将所有字符串放在小写形式的NSSetor中。NSMutableSet这将有效地处理您的问题,因为这正是NSSets 存在的原因。

于 2012-05-04T09:05:43.310 回答
1

使用该compare:options:NSCaseInsensitiveSearch:方法NSString应该可以工作。

if ([@"A" compare:@"a" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
    NSLog(@"same");
}
于 2012-05-04T09:07:36.973 回答