ios 如何在目标c中找到数组索引值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14436549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:56:20  来源:igfitidea点击:

how to find the Array index value in objective c

iphoneiosxcode

提问by kannan

Possible Duplicate:
Getting Index of an Object from NSArray?

可能的重复:
从 NSArray 获取对象的索引?

I have one Array and one string. Array name is "Array1", String name is "String", Now i have this below Values in "Array1".

我有一个数组和一个字符串。数组名称是“Array1”,字符串名称是“String”,现在我在“Array1”中的值下面有这个。

"array1 = [[NSMutableArray alloc]initWithObjects:@"A",@"b",@"c",@"d",@"e", nil];"

My String value is "A"

我的字符串值为“A”

Now i want to compare these string and array. And i want to display the Array index value.

现在我想比较这些字符串和数组。我想显示数组索引值。

How can i do this. Please somebody help me. Thanks in advance.

我怎样才能做到这一点。请有人帮助我。提前致谢。

回答by Andrey Chernukha

i'm not sure if this is what you're looking for but maybe:

我不确定这是否是您要找的,但也许:

int index = [array1 indexOfObject:String];

回答by Buntylm

int i;
for (i = 0; i < [myArray count]; i++) {
  id myArrayElement = [myArray objectAtIndex:i];
...do something useful with myArrayElement
 }`

You can Iterate NSMutableArray and to find the location of particular Object use

您可以迭代 NSMutableArray 并找到特定对象使用的位置

 int indexValue = [myArray indexOfObject:yourString];

回答by Anusha Kottiyal

Try this,

尝试这个,

if([array1 containsObject: yourString])
{
   int index = [array1 indexOfObject:yourString];
}

回答by Anoop Vaidya

Use

NSString *String=@"A";
NSInteger index = [array1 indexOfObject:String]; 

NOTE : Never use variable name with capital String, it should be string.

注意:永远不要使用大写的变量名String,它应该是string

回答by who9vy

Checking

检查

int index = [yourArray indexOfObject:yourObject];

is correct for NSStringobjects and some other types of objects.

对于NSString对象和其他一些类型的对象是正确的。

When you have implemented a class on your own and you want to get the index of an object within an array you have to implement the method

当您自己实现了一个类并且想要获取数组中对象的索引时,您必须实现该方法

- (BOOL)isEqual:(id)anObject