简体   繁体   中英

Difference Between [[CAShapeLayer alloc] init] and [CAShapeLayer layer]

I noticed that most people, when initializing a CAShapeLayer use:

CAShapeLayer *shapeLayer = [CAShapeLayer layer];

Rather than using the initializer:

CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];

I'm wondering is there any particular difference in using either one of these, or which one is usually better?

The first one returns an autoreleased object. Since it's been autoreleased, you aren't an owner of it. The object will get released automatically for you, when the autorelease pool (in which it resides) gets released.

The second one returns an object with +1 retain count. You are an owner of that object and hence are responsible to release it. However - With ARC you may not need to call release as it does for us.

Related Links:

  1. Objective C Method Families
  2. Basic Memory Management Rules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM