简体   繁体   中英

Xcode auto-complete suggests mysterious “songsAtIndexes” for NSArray getter

I have a property of type NSArray on my class called "songs". I'm creating a custom getter for it and XCode gives me an option of creating a method:

songsAtIndexes:(NSIndexSet *)indexes

What is this and why is XCode offering this? Is this specific to NSArray properties? What is the purpose of creating a method/getter for this method? If I don't define it manually, will it be automatically created/synthesized?

This is the result of a little-used KVC optimization for indexed collections which can be used on your class. You can read about this here , but to excerpt:

Indexed To-Many Relationship Compliance

For indexed to-many relationships, KVC compliance requires that your class:

  • Implement a method named -<key> that returns an array.
  • Or have an array instance variable named <key> or _<key> .
  • Or implement the method -countOf<Key> and one or both of -objectInAtIndex: or -<key>AtIndexes: .
  • Optionally, you can also implement -get<Key>:range: to improve performance.

It's only really used with Core Data with KVC (and occasionally NSPredicate s), but you can leverage these methods if you'd like to.

It's really not necessary in 99% of cases to implement this, but you can if you'd like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM