有没有更好的方法来比较一个字符串与多个字符串?

发布于 2024-09-11 12:12:18 字数 267 浏览 3 评论 0原文

我想将一个字符串与多个字符串进行比较。例如,

if([var isEqualToString:@"Box"]||[var isEqualToString:@"Ball"]|[varisEqualToString:@"Bat"])
{
some function
}else{
some function
}

在我的例子中,我必须与 15 个字符串进行比较,所以我必须检查 15 次。是否有其他更好的方法来比较它。是否有任何简单的小代码可以实现我的逻辑。

I want to compare a string with multiple string.For ex

if([var isEqualToString:@"Box"]||[var isEqualToString:@"Ball"]|[varisEqualToString:@"Bat"])
{
some function
}else{
some function
}

In my case I have to compare with 15 string, so I have to check for 15 times.Is there any other better way to compare it. Is there any small simple code will implement my logic.

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

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

发布评论

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

评论(3

烈酒灼喉 2024-09-18 12:12:18

您最好将字符串添加到 NSSet 中,如下所示:

NSSet *mySet = [NSSet setWithObjects:@"Box", @"Ball", @"Bat", nil];
if([mySet containsObject:string]) {
} else {
}

许多其他解决方案使用数组或字典来实现相同目的。集合是正确的数据结构,因为它们是为了包含无序对象和测试成员资格而创建的。我非常确定 containsObject: 与需要进行元素搜索的 NSArray 中的相同方法相比,运行时间恒定。

You're better off adding the strings to an NSSet as follows:

NSSet *mySet = [NSSet setWithObjects:@"Box", @"Ball", @"Bat", nil];
if([mySet containsObject:string]) {
} else {
}

A lot of the other solutions use arrays or dictionaries for the same purpose. Sets are the correct data structure for this since they are created for the purpose of containing unordered objects and testing membership. I'm pretty sure containsObject: runs in constant time compared to the same method in NSArray which needs to do an element search.

旧时光的容颜 2024-09-18 12:12:18

将字符串放入 NSDictionary 中:

NSNull *nullValue = [NSNull null];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:nullValue, nullValue, ..., nil forKeys:@"Box", @"Ball", ..., nil];
if ([dictionary objectForKey:var]) {
    // var matches one of the keys, run function 
}
else { 
    // var doesn't match any of the keys, do something else
}

字典查找的时间复杂度为 O(1),而数组搜索的时间复杂度可能为 O(log n)。对于 15 个元素来说没什么大不了的,但作为一般规则,字典或集合可能会表现得更好。如果您经常进行这种搜索/比较,需要考虑一些事情。

编辑

正如我所提到的, NSSet 也会在 O(1) 时间内进行查找:

NSSet *comparisonSet = [NSSet setWithObjects:@"Box", @"Ball", ..., nil];
if ([comparisonSet containsObject:var]) {
    // var matches set object, run function
} 
else {
    // var doesn't match any of the set objects, do something else
}

绝对是更干净的代码,但我认为 NSSet 实例需要创造时间要长得多。但这样你就只需要做一次,对吗?

Put your strings into an NSDictionary:

NSNull *nullValue = [NSNull null];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:nullValue, nullValue, ..., nil forKeys:@"Box", @"Ball", ..., nil];
if ([dictionary objectForKey:var]) {
    // var matches one of the keys, run function 
}
else { 
    // var doesn't match any of the keys, do something else
}

Dictionary lookups are O(1), whereas an array search is probably O(log n). Not a big deal for 15 elements, but as a general rule a dictionary or set will likely perform better. Something to think about if you do this search/comparison a lot.

EDIT

As I mentioned, an NSSet will also do lookups in O(1) time:

NSSet *comparisonSet = [NSSet setWithObjects:@"Box", @"Ball", ..., nil];
if ([comparisonSet containsObject:var]) {
    // var matches set object, run function
} 
else {
    // var doesn't match any of the set objects, do something else
}

Cleaner code, definitely, but I think NSSet instances take much longer to create. But then you only have to do it once, right?

魂归处 2024-09-18 12:12:18

您可以使用创建一个数组,

NSArray *stringArray = [NSArray arrayWithObjects: @"Box", @"Ball", @"Bat", nil];
if([NSArray indexOfObject:var] != NSNotFound)
{
   ...
}
else
{
   ...
}

虽然不是更好,但可能更具可读性。

You could create an array using

NSArray *stringArray = [NSArray arrayWithObjects: @"Box", @"Ball", @"Bat", nil];
if([NSArray indexOfObject:var] != NSNotFound)
{
   ...
}
else
{
   ...
}

Not really better, but possibly more readable.

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