测试 NSMutableArray 是否包含字符串对象

发布于 2024-08-29 19:32:56 字数 124 浏览 0 评论 0原文

我有一个 NSMutableArray ,其中包含一些 NSString 对象。如何测试数组是否包含特定的字符串文字?

我尝试了 [array containsObject:@"teststring"] 但这不起作用。

I have a NSMutableArray which contains a few NSString objects. How can I test if the array contains a particular string literal?

I tried [array containsObject:@"teststring"] but that doesn't work.

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

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

发布评论

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

评论(5

沐歌 2024-09-05 19:32:56

你所做的应该很好。例如,

NSArray *a = [NSArray arrayWithObjects:@"Foo", @"Bar", @"Baz", nil];
NSLog(@"At index %i", [a indexOfObject:@"Bar"]);

为我正确记录“在索引 1”。两个可能的缺点:

  1. indexOfObject 发送 isEqual 消息来进行比较 - 您还没有在类别中替换此方法吗?
  2. 确保您正在测试 NSNotFound 是否无法定位,而不是(比如)0。

What you're doing should work fine. For example

NSArray *a = [NSArray arrayWithObjects:@"Foo", @"Bar", @"Baz", nil];
NSLog(@"At index %i", [a indexOfObject:@"Bar"]);

Correctly logs "At index 1" for me. Two possible foibles:

  1. indexOfObject sends isEqual messages to do the comparison - you've not replaced this method in a category?
  2. Make sure you're testing against NSNotFound for failure to locate, and not (say) 0.
远昼 2024-09-05 19:32:56
[array indexOfObject:object] != NSNotFound
[array indexOfObject:object] != NSNotFound
瑾兮 2024-09-05 19:32:56

与字符串文字进行比较仅适用于代码示例。在现实世界中,您经常需要与数组中的 NSString* 实例进行比较,在这种情况下 containsObject 会失败,因为它与对象而不是值进行比较。

您可以在您的实现中添加一个类别,该类别扩展了 NS(Mutable)Array,并带有一个方法来检查它是否包含字符串(或您需要比较的任何其他类型);

@implementation NSMutableArray (ContainsString)
-(BOOL) containsString:(NSString*)string
{
  for (NSString* str in self) {
    if ([str isEqualToString:string])
      return YES;
  }
  return NO; 
}
@end

Comparing against string literals only works in code examples. In the real world you often need to compare against NSString* instances in e.g. an array, in which case containsObject fails because it compares against the object, not the value.

You could add a category to your implementation which extends NS(Mutable)Array with a method to check wether it contains the string (or whatever other type you need to compare against);

@implementation NSMutableArray (ContainsString)
-(BOOL) containsString:(NSString*)string
{
  for (NSString* str in self) {
    if ([str isEqualToString:string])
      return YES;
  }
  return NO; 
}
@end
莫言歌 2024-09-05 19:32:56

您还可以使用谓词:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", theArray];
BOOL result = [predicate evaluateWithObject:theString];

You may also use a predicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", theArray];
BOOL result = [predicate evaluateWithObject:theString];
鱼忆七猫命九 2024-09-05 19:32:56

对于每个对象

[(NSString *) [array objectAtIndex:i] isEqualToString:@"teststring"];

for every object

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