Xcode code completion surprises
Code completion in Xcode saves time, it's (usually) your best friend. Your class complies with the UITableViewDataSource protocol? Start typing "dash-space-t-a-b-l-e" and you'll see a list of different methods that you can implement.
Similarly, start typing a method name that exists in the superclass and the completion will allow you to quickly enter the correct signature to override the method.
One of my students asked me a question about some confusing suggestions for methods that don't exist in any declared protocols or superclass(es). Let's say you define a property:
@property (nonatomic, strong) NSString *myPropertyName;
When you start typing a new method name, code completion will offer very strange code completion options, in addition to the usual suspects:
If you accept one of these (e.g., removeMyPropertyName:) and try to command-click on it, you'll be notified that the symbol is not found. What's going on? Turns out, most of these are related to Key-Value Coding, and specifically to Collection Accessor Patterns for To-Many Properties.
With collection accessor methods, it's possible to use classes other than NSArray and NSSet for implementing to-many relationships. These methods can improve design and performance, especially in case of mutable to-many relationships.
Here's the list of extra methods that will be suggested for the property myPropertyName:
- (void)addMyPropertyName:(NSSet *)objects;
- (void)addMyPropertyNameObject:(object-type *)object;
- (NSUInteger)countOfMyPropertyName;
- (NSEnumerator *)enumeratorOfMyPropertyName;
- (void)getMyPropertyName:(object-type **)buffer range:(NSRange)inRange;
- (void)insertMyPropertyName:(NSArray *)array atIndexes:(NSIndexSet *)indexes;
- (void)insertObject:(object-type *)object inMyPropertyNameAtIndex:(NSUInteger)index;
- (void)intersectMyPropertyName:(NSSet *)objects;
- (object-type *)memberOfMyPropertyName:(object-type *)object;
- (NSArray *)myPropertyNameAtIndexes:(NSIndexSet *)indexes;
- (id)objectInMyPropertyNameAtIndex:(NSInteger)index;
- (void)removeMyPropertyName:(NSSet *)objects;
- (void)removeMyPropertyNameAtIndexes:(NSIndexSet *)indexes;
- (void)removeMyPropertyNameObject:(object-type *)object;
- (void)replaceMyPropertyNameAtIndexes:(NSIndexSet *)indexes withMyPropertyName:(NSArray *)array;
- (void)replaceObjectInMyPropertyNameAtIndex:(NSUInteger)index withObject:(id)object;
The choice of methods to implement depends on the requirements: is it an ordered or an unordered collection, is it mutable or immutable, etc. For more information, check Collection Accessor Patterns for To-Many Properties.