Saturday, July 26, 2008

Properties and attributes

I started reading about Cocoa Bindings, trying to be a little more systematic. The first chapter says:
Cocoa applications generally adopt the Model-View-Controller (MVC) design pattern. When you develop a Cocoa application, you typically use model, view, and controller objects, each of which performs a different function. Model objects represent data and are typically saved to a file or some other permanent data store. View objects display model attributes.
...
A binding is an attribute of one object that may be bound to a property in another such that a change in either one is reflected in the other. For example, the “value” binding of a text field might be bound to the temperature attribute of a particular model object.

So, we learn that models have attributes, but they never explain what the difference is between a property and an attribute. Does it have to do with models and views or with class variables and instance variables?

In the docs describing the Objective-C 2.0 language, there is a page which says
You can think of a property declaration as being equivalent to declaring two accessor methods. Thus, for example,
@property NSString *name;

is equivalent to:
- (NSString *)name;

- (void)setName:(NSString *)newName;

A property declaration, however, provides additional information about how the accessor methods are implemented (as described in “Property Declaration Attributes”).

So I gather that a property is like an instance variable, except that it implicitly refers to the the accessor methods (and how they work) as well.

I'm not so sure about attributes, but one place I've run into them before is with text. An NSAttributedString is, of course, a string with attributes, e.g. the font. So attributes are things about an object that control the way it acts. That's all the further I've got, and I'm still confused.