3

I have a following method to find index of a book object which have a book name:

+(NSInteger)indexXXX:(NSString *)bookName XXX:(NSArray *)books
{
  for (NSInteger i = 0; i < books.count; i++) {
    NSDictionary *book = [books objectAtIndex:i];
    if ([[book objectForKey:@"bookName"] isEqualToString:bookName]) {
      return i;
    }
  }
  return -1;
}

But I don't know what name should I use for this method.
Could you tell me some method names commonly used for this kind of method.

1 Answer 1

4

Usually we just name methods after what they do, so something like indexOfObjectWithName: inArray: would work.

To carry on past answering this question, the method can be made much simpler:

{
    NSArray *names = [books valueForKey: @"bookName"];
    return [names indexOfObject: bookName];
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.