Search code examples
cocoacocoa-bindings

Why does the name of my internal member variable break data binding?


So I am really new to cocoa programming. In fact I am very new to the Mac platform too. Still trying to get used to the fact that control+left arrow takes me to the beginning of the line.

Ok: So I am working through the tutorials in the book 'Cocoa Programming (4th edition) by Hillegass). So I got to chapter 9, which walks through creating a document view app, that uses a NSArrayControler to bind to a NSMutableArray of Person's.

The tutorial walked me through creating a sub-class of document, and adding a NSMutableArray pointer. So I took some liberty and named it mEmployee's instead of just employees.

@interface RMDocument : NSDocument
{
    NSMutableArray* mEmployees;
}
-(void) setmEmployees:(NSMutableArray*)a;

-(void) insertObject:(Person*)p inEmployeesAtIndex:(NSUInteger)index;
-(void) removeObjectFromEmployeesAtIndex:(NSUInteger)index;

-(void) startObservingPerson:(Person*) person;
-(void) stopObservingPerson:(Person*) person;

@end

Now when I did this, it seems the binding broke on the NSArrayController. So methods like setEmployee, insertObject and removeObject were never called.

Now I am still very new to objective-C, but I thought that mEmployee's was an internal member variable to my 'RMDocument' interface and that I could name it what-ever I want. I wanted to prefix the name with 'm' in order to distinguish it from other variable names (Kind of like member variables in C++). Apparently that was a big no no.

So why is the variable name had such a big effect?

I have placed the entire source for the project at: https://www.dropbox.com/sh/fq166ap3xzlw5xc/EZJXqIZPRY/RaiseMan

Thanks!


Solution

  • The name of your accessor method needs to follow the naming conventions: for a property "foo", the setter is "setFoo" (note the capitalization). So, you need to have setMEmployees, not setmEmployees.

    As a side note, your idea of prefixing member variables with "m" is not typical Cocoa style; it may make your code more difficult for others to read.