Re: property setters and getters with different types
Re: property setters and getters with different types
- Subject: Re: property setters and getters with different types
- From: Fritz Anderson <email@hidden>
- Date: Tue, 2 Mar 2010 16:17:05 -0600
On 2 Mar 2010, at 3:12 PM, Jean-Denis Muys wrote:
> I have a domain class with a number of fields, most of them NSString, but a few NSDate, int and BOOL, for example
>
> @interface Stuff : NSObject {
> NSString *name;
> NSDate *when;
> int *size;
> BOOL *grownUp;
> }
>
> @property (nonatomic, readonly) NSString *name;
> @property (nonatomic, readonly) NSDate *when;
> @property (nonatomic, readonly) int *size;
> @property (nonatomic, readonly) BOOL *grownUp;
>
> @end
Are you sure you mean to store pointers to your non-objects (int, BOOL) and not the values themselves? You know best, but it seems very odd.
> As you can see at this point, I have the "normal" getters. These objects are displayed (read only) in a table view, using the column identifier as the key for KVC. So far, so good. Now I'm going wild defining my setters:
>
> - (void) setName:(DOMNode *)aNode
> {
> // parse the DOM node to set the name
> }
>
> - (void) setWhen:(DOMNode *)aNode { ditto }
> - (void) setSize:(DOMNode *)aNode { ditto }
> - (void) setGrownUp:(DOMNode *)aNode { ditto }
>
> @end
You're right that this smells bad.
It should sort of work, for the limited purposes to which you put the setters, but at the very least it abuses the conventions of Cocoa programming: Accessors should be symmetric, especially if you express them through properties. Some services (like bindings) assume symmetry, and you're denying yourself the benefits of those services.
I'd provide set<Property>FromDOM: methods, which would express what you're actually doing. It would allow you to declare the properties readwrite so you can set them from other than DOMNodes (for instance through bindings), which you'll want to do if you want to edit them in a table. It also means that KVO notifications will be sent whenever name (etc.) actually changes, and not when a change is merely attempted (I assume the setName: you have could fail to parse).
Your initialization-from-DOM, if it must use computed keys, can setValue:forKey: with keys like @"<property>FromDOM". If you're doing something really nice with computed KVC keys, it may take some work to get the niceness back; but I don't know enough to comment.
I am sure that in my haste I have missed some major point. We'll hear about that soon enough.
— F
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden