Re: CALayer properties
Re: CALayer properties
- Subject: Re: CALayer properties
- From: Dimitri Bouniol <email@hidden>
- Date: Tue, 28 Oct 2008 17:11:28 -0700
Properties allow you to "drill down" and set values to the sub-properties that are objects (id or NSObject subclasses) of its own properties. However, the property called "frame" is a CGRect, a struct. Structs have the same syntax as properties, however it is wrong to assume they are the same thing.
So while you could reference the variables of the struct through properties like myLayer.frame.size.width, it would be equivalent to [myLayer frame].size.width .
However, when you want to set a property to something, myLayer.frame.size.width = 5 is completely different. This statement would be equivalent to calling [[[myLayer frame] size] setWidth:5], which is a reference to an object, and its methods.
If you want to set a value to a struct, you must pass a new struct containing the changed value, like myLayer.frame = CGRectMake(...), which is equivalent to [myLayer setFrame:CGRectMake(...)].
So if you are dealing with objects (id or NSObject subclasses), chaining them is fine in both the cases of setting and getting. But if you are working with a CGRect (or CGSize, CGPoint, NSRect ...), you must be careful to pass a new struct if it is in a property.
Correct Examples:
myObject.anotherObject.yetAnotherObject = anObject; ([[myObject anotherObject] setYetAnotherObject:anObject];)
myObject.anotherObject.yetAnotherObject.anInt = 5; ([[[myObject anotherObject] yetAnotherObject] setAnInt:5];)
anObject = myObject.anotherObject.yetAnotherObject; (anObject = [[myObject anotherObject] yetAnotherObject];)
anInt = myObject.anotherObject.yetAnotherObject.anInt; (anInt = [[[myObject anotherObject] yetAnotherObject] anInt];)
myObject.anotherObject.aRect = anotherRect; ([[myObject anotherObject] setARect:anotherRect];)
aFloat = myObject.anotherObject.aRect.size.width; (aFloat = [[myObject anotherObject] aRect].size.width;)
aSize.width = 5;
aPoint.x = myObject.anotherObject.yetAnotherObject.aFloat; (aPoint.x = [[[myObject anotherObject] yetAnotherObject] aFloat];)
I hope this explains it :)
--
定魅刀利
Dimitri Bouniol
email@hidden
http://www.appkainime.com/
On Tuesday, October 28, 2008, at 04:44PM, "DKJ" <email@hidden> wrote:
>When I did this:
>
> myLayer.frame.size.width = rootLayer.frame.size.width;
>
>I got an "illegal lvalue" error. But when I do this:
>
> CGRect r = help.frame;
> r.size.width = rootLayer.frame.size.width;
>
>the compiler says nothing. Why can I use layer.frame.size on the right
>of =, but not the left?
_______________________________________________
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