Bill Kary
Bill Kary

Reputation: 705

getting NSString Array index by value in objective C

I am new to objective C. I have created an NSString array by the following way:

static NSString* fontSizeName[] = 
{
 @"14",
 @"18",
 @"22",
 @"26",
 @"30",
 @"34",
};

Now, i have the value, "26", how can i get the index of it in fontSizeName[]?

Thanks for input.

Upvotes: 0

Views: 2632

Answers (2)

eykanal
eykanal

Reputation: 27017

Use the indexOfObject method:

index = [fontSizeName indexOfObject:@"26"];

Upvotes: 2

mipadi
mipadi

Reputation: 410552

NSString *val = @"26";    // Get this from somewhere
int i;
int idx = -1;
for (i = 0; i < 6; i++) {
    if ([fontSizeName isEqualToString:val]) {
        idx = i;
        break;
    }
}

Upvotes: 1

Related Questions