0

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.

2 Answers 2

2

Use the indexOfObject method:

index = [fontSizeName indexOfObject:@"26"];
Sign up to request clarification or add additional context in comments.

1 Comment

That would be great if fontSizeName was an NSArray, but it's an ordinary C array.
1
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;
    }
}

2 Comments

+1 A "nil" sentinel element in the array would make it more flexible though and wouldn't need to hardcode the length. Or use an NSArray instead with indexOfObject:.
@Eiko: I think you should make the NSArray part an answer. I was going to until I saw your comment.

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.