0
我已经看到了有关NSArray的indexOfObjectIdenticalTo:
方法的类似问题,但我希望我的问题只会产生简单的答案。搜索以编程方式构建的字符串是无用的吗?iOS indexOfObjectIdenticalTo:对字符串不起作用
NSArray* theArray = [[NSArray alloc] initWithObjects:@"John", nil];
NSString* searchString1 = @"John";
NSString* stringComponent = @"Jo";
NSString* searchString2 = [stringComponent stringByAppendingFormat:@"hn"];
BOOL testContains1 = [theArray containsObject:searchString1];
int testIndex1 = [theArray indexOfObjectIdenticalTo:searchString1];
BOOL testContains2 = [theArray containsObject:searchString2];
int testIndex2 = [theArray indexOfObjectIdenticalTo:searchString2];
在上面的例子,testContains1
和testContains2
都返回相同的值,这是true
。但testIndex1
和testIndex2
不同; testIndex1
为0,因为它在索引0处发现了与预期相同的对象,但不幸的是testIndex2
是2147483647(NSNotFound)。
谢谢!我混淆了两种方法,认为'indexOfObject:'寻找匹配的对象地址,但我应该仔细阅读文档。 – 2012-07-14 01:01:08