简体   繁体   English

[[CAShapeLayer alloc] init]和[CAShapeLayer层]之间的区别

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

I noticed that most people, when initializing a CAShapeLayer use: 我注意到大多数人在初始化CAShapeLayer使用时:

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. 第二个对象返回保留计数为+1的对象。 You are an owner of that object and hence are responsible to release it. 您是该对象的所有者,因此有责任release该对象。 However - With ARC you may not need to call release as it does for us. 但是-使用ARC,您可能不需要像我们这样调用release

Related Links: 相关链接:

  1. Objective C Method Families 目标C方法系列
  2. Basic Memory Management Rules 基本内存管理规则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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