Search code examples
iosxcodepropertiesautomatic-properties

iOS -- "add" methods appearing for autosynthesized properties in codeSense


I just created an iOS class with the following properties:

@property (nonatomic, strong) NSString* foo;
@property (nonatomic, strong) NSObject* bar;
@property (nonatomic) CGRect fubar;

I did not put in any @synthesize or explicit ivars for these properties. I then went into the implementation file and started to create a method as follows:

-(void) add

I left the cursor at the end of the word "add". The following method names then popped up in code sense:

addBar: (NSSet*) objects
addBarObject: (objectType *) object
addFoo: (NSSet*) objects
addFooObject: (objectType *) object
addFubar: (NSSet*) objects
addFubarObject: (objectType *) object

What are these methods? Are there any docs for them?


Solution

  • That are accessor methods that a class can implement to support Key-Value Coding for mutable to-many relationships, see Mutable Unordered Accessors in the "Key-Value Coding Programming Guide":

    In order to be key-value coding complaint for a mutable unordered to-many relationship you must implement the following methods:

    -add<Key>Object: or -add<Key>:. At least one of these methods must be implemented. These are analogous to the NSMutableSet method addObject:.

    The same "strange" autocompletion happens for other Key-Value coding accessor methods, for example:

    - (void)remove...
    - (void)intersect...
    - (NSUInteger)countOf...