2011-11-11 11 views
2
NSMutableArray*array = [[NSMutableArray alloc]init]; 

NSArray*Somearray = [NSArray arrayWithObjects:1st Object,2ndObject,3rd Object,4th object,5th Object,nil]; 

上記の配列では、第1オブジェクト、第2オブジェクト、第3オブジェクト、第4オブジェクト、第5オブジェクトの各インデックスにval、content、結論があります。NSArrayでインデックスを取得するには?

for(int i=0;i<[Somearray count];i++) 
{ 

______________ 

Here the code is there to give each index ,that is having val,content,conclusion .. 

After that val,content,conclusion in each index will be add to Dict.. 
____________ 


NSDictionary *Dict = [NSDictionary dictionaryWithObjectsAndKeys:val,@"val",content,@"content",conclusion,@"conclusion",nil]; 

//Each time adding dictionary into array; 

[array addObject:Dict]; 

} 

上記辞書forループ内にあり、キー値ペアが.Nowアレイは今私は今NSString*string = @"1.5";

を有してい

array = [{val="1.1 this is first one",content="This is the content of 0th index",conclusion="this is the conclusion of 0th index"},{val="1.2 this is first one",content="This is the content of 1st index",conclusion="this is the conclusion of 1st index"},____,____,______,{val="1.5 this is first one",content="This is the content of 4th index",conclusion="this is the conclusion of 4th index"},nil]; 

に有している(Somearrayカウント)5回を追加します私はvalがit.Howにインデックスを見つけるために配列にstrを送るために持っているインデックスが必要です。

誰でもコードを共有できますか。

ありがとうございます。

答えて

2
NSUInteger idx = UINT_MAX; 
    NSCharacterSet* spaceSet = [NSCharacterSet whitespaceCharacterSet]; 
    for(int i=0,i_l=[Yourarray count];i<i_l;i++) { 
     NSString* s_prime = [[Yourarray objectAtIndex:i] valueForKey:@"val"]; 
     if ([s_prime length] < 4) { 
      continue; 
     } 
     NSString *subString = [[s_prime substringToIndex:4] stringByTrimmingCharactersInSet:spaceSet]; 
     // NSLog(@"index %@",s); 
     if ([subString isEqualToString:secretNumber]){ 
      idx = i; 
      break; 
     } 
    } 
    if (idx != UINT_MAX) { 
     // NSLog(@"Found at index: %d",idx); 
    } else { 
    // NSLog(@"Not found"); 
    } 
6

あなたが探している方法は-[NSArray indexOfObjectPassingTest:]です。あなただけvalがあなたの代わりにhasPrefix:を使用することになり、「1.5」で始まることを確認したい場合は

NSUInteger i = [array indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) { 
     return [[id objectForKey:@"val"] rangeOfString:@"1.5"].location != NSNotFound; 
    }]; 

:あなたはこのようにそれを使用します。

+0

プレフィックスメソッドを持ってい – Univer

+0

'[[ id objectForKey:@ "val"] hasPrefix:@ "1.5"] '[NSString Class Reference](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class)を確認してください。 /Reference/NSString.html#//apple_ref/occ/instm/NS String/hasPrefix:)*。 –

12

使用方法は、検索インデックスの特定のキー値について

int inx= [array indexOfObject:@"1.5"]; 

をindexOfObject。

int inx; 
for (int i=0; i<[array count]; i++) { 
    if ([[[array objectAtIndex:i] allKeys] containsObject:@"val"]) { 
     inx=i; 
     break; 
    } 
} 
+0

Nice and simple –

+0

+1 from me ... cheetey;) – Devarshi

+0

配列には文字列ではなく辞書が含まれています。彼は、 "val"キーの値が "1.5"である辞書の索引を探したい。この答えはそれをしません。 –

6

これを試してみてください -

NSArray *valArray = [array valueForKey:@"val"]; 
int index = [valArray indexOfObject:@"1.5"]; 

あなたにキー値コーディングの魔法を表示するには、Mandeepによって与えられた別記答え;)を使用する方法

関連する問題