简体   繁体   English

CAShapeLayer双指针

[英]CAShapeLayer double pointer

I'm trying to declare a CAShapeLayer **dot_p; 我正在尝试声明CAShapeLayer **dot_p; but Xcode is giving me the following error: 但是Xcode给我以下错误:

Pointer to non-const type 'CAShapeLayer *' with no explicit ownership

What is going on? 到底是怎么回事?

As you may know, Objective-C uses an automatic reference counting system (ARC) for memory management. 您可能知道,Objective-C使用自动引用计数系统(ARC)进行内存管理。 In order for that to work, it needs to understand the implicit ownership rules for any pointer so it knows if setting the pointer needs to increment the retain count for the object. 为了使其正常工作,它需要了解任何指针的隐式所有权规则,以便知道设置指针是否需要增加对象的保留计数。

ARC has sensible defaults, so if you say: ARC具有合理的默认值,因此,如果您说:

CAShapeLayer *foo;

It assumes that you want a strong reference. 它假定您想要一个强有力的参考。 So the above is equivalent to writing: 因此,以上内容等同于编写:

__strong CAShapeLayer *foo;

But if you have a pointer to a pointer, it isn't able to guess what the ownership rules should be. 但是,如果您有一个指向指针的指针,它就无法猜测所有权规则应该是什么。 If you want ARC to increment the reference count of your CAShapeLayer when the dot_p pointer is set, you could write: 如果希望在设置dot_p指针时ARC增加CAShapeLayer的引用计数,则可以编写:

CAShapeLayer *__strong *dot_p;

If you don't want it to increment the retain count you could write: 如果您不希望它增加保留计数,则可以编写:

CAShapeLayer *__weak *dot_p;

Or 要么

CAShapeLayer *__unsafe_unretained *dot_p;

If you aren't sure whether you want it to be strong or not, then I'd suggest thinking very hard about the ownership rules in your app, otherwise you're very likely to introduce a retain cycle (memory leak) or dangling pointer (crash). 如果您不确定自己是否想要强壮,那么我建议您认真考虑一下应用程序中的所有权规则,否则您很可能会引入保留周期(内存泄漏)或悬空指针(崩溃)。

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

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