2
NSMutableArray*array = [[NSMutableArray alloc]init];

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

In the above array 1st Object,2ndObject,3rd Object,4th object,5th Object having val,content,conclusion in each index.

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];

}

The above Dictionary is in for loop and the keyvalue pairs will be add 5 times(Somearray Count).Now array is having in

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];

Now i am having NSString*string = @"1.5";

Now i need the index where val is having 1.5 in it.How to send the str in to array to find the the index.

Can anyone share the code please.

Thanks in advance.

4 Answers 4

12

Use method indexOfObject

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

For Find index particular key value.

int inx;
for (int i=0; i<[array count]; i++) {
    if ([[[array objectAtIndex:i] allKeys] containsObject:@"val"]) {
        inx=i;
         break; 
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

The array contains dictionaries, not strings. He wants to find the index of the dictionary whose "val" key has a value containing "1.5". This answer won't do that.
6

The method you are looking for is -[NSArray indexOfObjectPassingTest:]. You would use it like this:

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

If you just want to check that val starts with "1.5" you would use hasPrefix: instead.

1 Comment

[[id objectForKey:@"val"] hasPrefix:@"1.5"] Check the NSString Class Reference.
6

Try this -

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

Appended answer given by Mandeep, to show you the magic of key value coding ;)

Comments

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");
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.