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

≡放荡痞女 提交于 2020-06-27 16:25:10

问题


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?


回答1:


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 NSPredicates), 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.



来源:https://stackoverflow.com/questions/15558964/xcode-auto-complete-suggests-mysterious-songsatindexes-for-nsarray-getter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!