Reputation: 447
I am trying to use CAShapeLayer
and draw some Bezier curve or circle on the UIImageView
I read some an example on Apple sample code and found something that I don't understand.
//for this line
CAShapeLayer* shapeLayer = [CAShapeLayer layer];
is there any difference from:
CAShapeLayer* shapeLayer = [CAShapeLayer alloc];
Obviously, if I use the latter, then I need to do [shapeLayer release]
(assume Automatic Reference Accounting is turned off)
Any suggestions would be appreciated
Upvotes: 0
Views: 351
Reputation: 437592
FYI, [CAShapeLayer layer]
is more analogous to [[CAShapeLayer alloc] init]
, not just [CAShapeLayer alloc]
. Or, in non-ARC world, it's actually equivalent to
[[[CAShapeLayer alloc] init] autorelease]
.
This pattern, a simple class convenience method that effectively does alloc
/init
is very common. Consider [NSMutableArray array]
instead of [[NSMutableArray alloc] init]
or [NSMutableDictionary dictionary]
instead of [[NSMutableDictionary alloc] init]
. You'll also often see variations of this convenience method take parameters. For example, consider NSString
methods like stringWithFormat
, stringWithContentsOfURL
, etc.
In terms of why you don't have to explicitly release objects that you create with these convenience methods, please note that the memory management is indicated by the method name. Methods that start with alloc
, copy
, new
, or mutableCopy
create +1 objects that you are responsible for releasing in a non-ARC environment. All other other methods generate autorelease
objects, which will be released for you when the pool is drained (unless, of course, you do something else that retains it). Bottom line, [CALayer layer]
generates an autorelease object and [[CALayer alloc] init]
does not.
Upvotes: 2
Reputation: 48514
Do not read this: it is obsolete technology! You are wasting your time writing non-ARC code.
+layer
is the equivalent of:
[[[CALayer alloc] init] autorelease];
Upvotes: 0
Reputation: 1152
I think [CAShapeLayer layer] actually use factory design pattern, it will create a layer for you. And by using [CAShapeLayer alloc], you will create a layer by yourself
Upvotes: 0