I want to create a property in a category so I can use dot notation with the accessors. The reason I want to do this is so I can refactor some existing code that references properties of an NSMangedObject. Then I will put the category accessor methods between the outside callers and the NSManagedObject properties that they relate to. (I don’t want to replace the current dot-notation usage with messaging, because I use dot notation through a very large project.)
I experimented a little inside the category interface with a property declaration like this:
@property NSString* details;
This results in a compiler warning (It didn’t like the default assign attribute.)
Since there is no ivar directly tied to the property, there is no sense in adding the attribute of retain or copy.
Next, I just left out the @property line and added accessors to the interface and implementation files.
- (void) setDetails:(NSString*)details;
- (NSString*) details;
I compiled and ran the code. I tested the methods using dot notation. I could see that the accessors could be successfully used without error and without the compiler complaining.
What I have read about dot-notation tells me that if I set up a property a certain way, I can use dot notation. But I am not setting up a property conforming to that requirement. But it appears that I can still use dot notation.
Is there anything wrong with this approach? Is there a specification that defines how and when dot notation accessors can be used?
The dot notation is intended to be used for declared property access, but in fact the compiler handles it loosely enough that any no-argument method can be used if you’re not assigning to the result:
Likewise, the compiler will allow you to use a dot-syntax expression as if it were an lvalue, if there is a one-argument method of the form
set<uppercase name>:available on the object:even if that name wasn’t declared with the
@propertysyntax.Blah, blah, I guess you knew that already. The only relevant documentation I can dig up at the moment is this sentence from TOCPL: