如何搜索字符串数组

发布于 2024-12-11 10:35:08 字数 595 浏览 0 评论 0原文

我正在创建一个应用程序,用户可以从表中选择多个单元格,并且每次选择一个单元格时,它都会被添加到 NSMutableArray 中。该表包含不同国家的名称。选择所有国家/地区后,如何搜索选择了哪些国家/地区?例如,如何检查用户是否选择了美国?

我的表的 didSelectRow 中有这个:

if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
        [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [listofCountriesselected addObject:[NSNumber numberWithInt:indexPath.row]];

    } 
    else {
        [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
        [listofCountriesselected removeObject:[NSNumber numberWithInt:indexPath.row]];

    }

I am creating an application where the user can select multiple cells from a table, and each time a cell is selected, it is being added to an NSMutableArray. The table contains names of different countries. Once all the countries are selected, how do I search for which countries are selected? For example, how do I check if the user selected United States for example?

I have this in my didSelectRow for my table:

if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
        [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [listofCountriesselected addObject:[NSNumber numberWithInt:indexPath.row]];

    } 
    else {
        [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
        [listofCountriesselected removeObject:[NSNumber numberWithInt:indexPath.row]];

    }

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

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

发布评论

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

评论(1

此刻的回忆 2024-12-18 10:35:08

您可以将国家/地区添加到 NSMutableArray 并检查 containsObject。在本例中,我添加一个字符串 - 您可以通过某个国家/地区标识符添加,但需要通过相同的国家/地区标识符(无论您选择什么)进行检查。行号不是一个好的标识符——它不稳定。

NSMutableArray *list = [[NSMutableArray alloc] init];
[list addObject:@"US"];
[list addObject:@"CN"];
NSLog(@"usa? %d", (int)[list containsObject:@"US"]);

检查某个国家/地区是否已被选择的更快方法是在选择该国家/地区时将其添加到 NSMutableSet 中。

NSMutableSet 是一个哈希集,因此查找速度非常快。

NSMutableSet *lookup = [[NSMutableSet alloc] init];
[lookup addObject:@"US"];
[lookup addObject:@"CN"];

NSLog(@"usa? %d", (int)[lookup containsObject:@"US"]);

两者都输出 1。

关键区别是 NSMutableArray 是项目的顺序列表 - 列表。 NSMutableSet 针对包含进行了优化 - 只是一个集合。例如,如果您不仅想跟踪选择的内容,还想跟踪选择它们的顺序,那么您需要一个可变数组。应用程序包含聚合数据结构来回答排序和包含类型问题也并不罕见。

顺便说一句,这是获取所有 ISO 国家/地区代码的方法

NSArray *countryCodes = [NSLocale ISOCountryCodes];
for (NSString *cc in countryCodes)
{
    NSLog(@"cc: %@", cc);
}

You can add the countries to the NSMutableArray and check containsObject. In this case, I'm adding a string - you can add via some country identifier but you need to check by the same country identifier (whatever you choose). Row number is not a good identifier - it's not stable.

NSMutableArray *list = [[NSMutableArray alloc] init];
[list addObject:@"US"];
[list addObject:@"CN"];
NSLog(@"usa? %d", (int)[list containsObject:@"US"]);

An even faster way to check if a country has been selected would be to add the country to an NSMutableSet when selected.

An NSMutableSet is a hash set so look ups are very fast.

NSMutableSet *lookup = [[NSMutableSet alloc] init];
[lookup addObject:@"US"];
[lookup addObject:@"CN"];

NSLog(@"usa? %d", (int)[lookup containsObject:@"US"]);

Both output 1.

The key difference is the NSMutableArray is an order list of items - a list. The NSMutableSet is optimized for contains - just a set. For example, if you wanted to not only track what's selected but the order they selected them in, then you need a mutable array. It's also not uncommon for apps to contain aggregate data structures to answer ordering and contains type questions.

BTW, here's how you can get all the ISO country codes

NSArray *countryCodes = [NSLocale ISOCountryCodes];
for (NSString *cc in countryCodes)
{
    NSLog(@"cc: %@", cc);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文