2013-01-24 39 views
1

我有一个数组,它是数组的集合。 说 ( (1,X,Y,a)中, (2,M,N,O), (3,S,T,U,V,W) )比较并从NSMutableArray获得索引

如果我想得到m的索引,我怎么得到它?

可以这样做使用NSMutableArray包含:(id)

回答

1
NSArray *arrWithArr; 
NSArray *arrWithM; 
NSInteger arrIndex; 
for (int i = 0; i< [arrWithArr count]; i++) { 
    if ([[arrWithArr objectAtIndex:i] containsObject:@"m"]) { 
     arrIndex = i; break; 
    } 
} 
NSInteger mIndex = [[arrWithArr objectAtIndex:arrIndex]indexOfObject:@"m"]; 
+0

完美,可以得到数组索引和元素索引。谢谢 – indiaxxo

1

你正在谈论的功能“[NSMutableArray contains:]”会告诉你对象“B”是否存在于{A,B,C}的数组中。

你真正需要做的是得到第二个函数,通过父数组快速枚举,然后在子数组上执行“contains”。如果找到“m”,则返回“YES”,并返回父数组中该条目的索引。

+0

是的,我以这种方式确实写,我得到了下面的答。 非常感谢。 – indiaxxo

2

检查与此代码:

int arrayPos = 0; 
for (NSArray *tempArray in collectionArray) 
{ 
    if(tempArray containsObject:@"m") 
    { 
    int indexOfWord = [tempArray indexOfObject:@"m"]; 
    NSlog(@"Array pos: %d Index is : %d",pos, indexOfWord); 
    } 
    pos++; 
} 

在for循环中你所收集阵列中添加的每个阵列。之后,您将检查数组中是否存在该单词。如果它存在,则显示它。 也打印数组的位置。

元素位于:

NSString *str = [[collectionArrayObjectAtIndex:pos] objectAtIndex:indexOfWord];