Usman Ali
Usman Ali

Reputation: 145

How to store index of a for loop in an array Xcode

I have a for loop running on string length. the first loop looks for a character common in the second loop. I want to save the index of nameString that is common in an array or a string. Please help. Code is mentioned below.

for (int i=0; i < nameString.length; i++) {

        char currentLetter = [nameString characterAtIndex:i];

    for (int j=0; j < partnersNameString.length; j++) {
        if (currentLetter == [partnersNameString characterAtIndex:j]) {

            NSRange range;
            range.length=1;
            range.location=j;
           [partnersNameString replaceCharactersInRange:range withString:@""];
            calculateField.text = partnersNameString;
        }
      }
    }

Upvotes: 1

Views: 704

Answers (3)

AppleDelegate
AppleDelegate

Reputation: 4249

[myArray addObject:[NSNumber numberWithInt:i]];

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

There are several ways to go about doing what you are looking for: you can use a "plain" C array with a count, or you can use an NSMutableArray with wrappers.

The first way would look like this:

NSUInteger indexes[MAX_LENGTH];
NSUInteger lastIndex = 0;
for (int i=0; i < nameString.length; i++) {
    char currentLetter = [nameString characterAtIndex:i];
    for (int j=0; j < partnersNameString.length; j++) {
        if (currentLetter == [partnersNameString characterAtIndex:j]) {
            indexes[lastIndex++] = i;
            // Go through the rest of your code
            ...
        }
    }
}
for (NSUInteger i = 0 ; i != lastIndex ; i++) {
    NSLog(@"Found match at index %u", indexes[i]);
}

The second way looks similar, except now you need to use NSNumber to wrap the data going into NSMutableArray:

NSMutableArray *indexes = [NSMutableArray array];
for (int i=0; i < nameString.length; i++) {
    char currentLetter = [nameString characterAtIndex:i];
    for (int j=0; j < partnersNameString.length; j++) {
        if (currentLetter == [partnersNameString characterAtIndex:j]) {
            [indexes addObject[NSNumber numberWithInt:i]];
            // Go through the rest of your code
            ...
        }
    }
}
for (NSNumber in indexes) {
   NSLog(@"Found match at index %i", [[indexes objectAtIndex:i] intValue]);
}

Upvotes: 1

Dmitry Zheshinsky
Dmitry Zheshinsky

Reputation: 569

You can use NSMutablearray for that

NSMutableArray myArray = [NSMutableArray new];
for (int i=0; i < nameString.length; i++) {

        char currentLetter = [nameString characterAtIndex:i];

    for (int j=0; j < partnersNameString.length; j++) {
        if (currentLetter == [partnersNameString characterAtIndex:j]) {
            //And add it here
            [myArray addObject:currentLetter];
            NSRange range;
            range.length=1;
            range.location=j;
           [partnersNameString replaceCharactersInRange:range withString:@""];
            calculateField.text = partnersNameString;
        }
      }
    }

Upvotes: 1

Related Questions