测试NSMutableArray是否包含字符串对象 [英] Testing if NSMutableArray contains a string object

查看:379
本文介绍了测试NSMutableArray是否包含字符串对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

推荐答案

你正在做什么应该可以正常工作。例如

What you're doing should work fine. For example

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

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

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


  1. indexOfObject 发送 isEqual 要进行比较的消息 - 您没有在类别中替换此方法吗?

  2. 确保您正在测试 NSNotFound 找不到,而不是(比如说)0。

  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.

这篇关于测试NSMutableArray是否包含字符串对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆