问题
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