Re: properties attributes misunderstanding
Re: properties attributes misunderstanding
- Subject: Re: properties attributes misunderstanding
- From: Colin Howarth <email@hidden>
- Date: Wed, 30 Sep 2009 15:59:12 +0200
Thanks Sherm, and Roland.
All clear. :-)
I'll leave it with
path = [ray.path copy]; // I don't do retain count stuff :-)
for now.
But I'm left wondering, from a clean coding and/or language design
point of view, is it *obvious* that the copy attribute would apply to
setting as opposed to reading? Sometimes you want the actual object
(ie. the reference or pointer to it) and sometimes you want a copy of
it (ie. a new reference / pointer).
I suppose I still have the procedural call by reference / call by
value idea in the back of my mind. There (in C) you get a copy 'by
default',
(Sorry if I mix up terms, I mostly learnt OO in perl, with a bit of
Modula-2/Oberon lurking in the background and C and asm ever-present.
Can't stand C++ or java though.)
colin
On 30 Sep, 2009, at 15:16, Roland King wrote:
Other way around. The copy happens when you *set* the property.
So ray.path = OriginalPathPointerYouStartedWith;
will copy OriginalPathPointerYouStartedWith and then set ray's
member variable to that copy. However when you READ the property, it
just returns what's in the member variable over and over again.
If you wanted it to return a copy you could if you wish write your
own accessor
-(NSBezierPath*)path
{
return( [ [ path copy ] autorelease ] );
}
which would copy it when it's accessed and give the accessor its own
one to play with. So it would be copied when you set it, and then
copied when you returned it. I'm not totally sure how much I like
that, it feels a little semantically wrong to me but in the context
of your class that might make sense. Were I really going to do that
I may not use a property at all and might have a method which is
called
(NSBezierPath*)copyPath
which explicitly returns a copy of the path because that's clear.
On 30-Sep-2009, at 9:02 PM, Colin Howarth wrote:
Hi,
I have a class
@interface Ray : NSObject {
...
NSBezierPath *path;
...
}
@property (copy) NSBezierPath *path;
@end
an instance of which is accessed in another class:
- (void)drawRect:(NSRect)rect
{
NSBezierPath *temporaryPathPointerWhichIsSupposedToBeDifferent;
...
temporaryPathPointerWhichIsSupposedToBeDifferent = [ray.path];
[temporaryPathPointerWhichIsSupposedToBeDifferent
transformUsingAffineTransform: aVeryLongDescriptiveTransformName];
[temporaryPathPointerWhichIsSupposedToBeDifferent stroke];
...
}
I thought the (copy) attribute would mean that
temporaryPathPointerWhichIsSupposedToBeDifferent would be, well, a
copy of the original path instance (which conforms to the NSCopying
protocol). Of course, the compiler doesn't care what *I* think
should happen, and applies the transform to path (which is
identical to temporaryPathPointerWhichIsSupposedToBeDifferent).
It works if I do:
temporaryPathPointerWhichIsSupposedToBeDifferent = [ray.path copy];
instead.
Why isn't it doing what I think it should do though?
Thanks,
colin
_______________________________________________
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
_______________________________________________
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