__strong 和 __weak 关键字放置 - Objective-C

发布于 2024-12-27 19:35:09 字数 443 浏览 5 评论 0原文

编译器似乎对以下两个声明没有问题:

NSObject * __weak weakThing;
__weak NSObject *anotherWeakThing;

两者之间有区别吗?行为是否类似于 const 关键字< /a>?

我问这个问题是因为 Xcode 的警告通常表明......

SomeDataType * __weak / __strong

...当你搞砸了一些事情时。所以我尝试遵循这种模式,但想知道是否有什么不同。

The compiler seems to have no problem with the two following declarations:

NSObject * __weak weakThing;
__weak NSObject *anotherWeakThing;

Is there a difference between the two? Is the behavior like the const keyword?

I ask because Xcode's warning generally suggest ...

SomeDataType * __weak / __strong

... when you've goofed something up. So I've tried to follow this pattern, but wondered if there was a difference at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2025-01-03 19:35:09

不,没有区别。使用 const 关键字,它可以在声明中应用多种内容;它可以应用于指针,也可以应用于所指向的值。

所有权限定符仅对指向对象的指针有意义。物体本身不可能是“强”或“弱”;它是指向强对象或弱对象的指针。 ARC 仅在直接应用于对象指针类型时才有意义,并且会影响指针的生命周期如何影响对象的生命周期。

鉴于所有权限定符的适用范围从来不存在任何歧义,ARC 规范允许将所有权限定符放置在指向对象的指针定义中的任何位置。你的两个例子都同样有效。同样,以下所有内容都表示同一件事:

NSError * __autoreleasing * someObject;
NSError __autoreleasing ** someObject;
__autoreleasing NSError ** someObject;

但请注意,编译器会抱怨这一点:

NSError ** __autoreleasing someObject;

这是因为您已经超出了指向对象的指针的定义。您可以将其解析为 (NSError *)* __autoreleasing someObject;。当您到达第二个 * 时,您已经定义了指针的类型,因此 __autoreleasing 没有任何意义。指针类型定义中的任何位置都可以,但是一旦转移到指针到指针类型,那么您就引用了其他内容,并且它不再有意义。

No, there is no difference. With the const keyword, there are multiple things it could apply to in a declaration; it could apply to the pointer, or it could apply to the value being pointed to.

Ownership qualifiers only make sense on pointers to objects. The object itself can't be "strong" or "weak"; it's the pointer to the object that is strong or weak. ARC only makes sense when applied directly to pointer-to-object types, and affects how that pointer's lifetime will affect the lifetime of the object.

Given that there is never any ambiguity about what the ownership qualifier could apply to, the ARC specification allows placement of the ownership qualifier anywhere in the definition of the pointer-to-object. Both of your examples are equally valid. Likewise, all of the following mean the same thing:

NSError * __autoreleasing * someObject;
NSError __autoreleasing ** someObject;
__autoreleasing NSError ** someObject;

Note that the compiler complains about this one, though:

NSError ** __autoreleasing someObject;

This is because you've moved beyond the definition of the pointer-to-object. You could parse that one as (NSError *)* __autoreleasing someObject;. By the time you get to the second *, you've already defined the type of the pointer, so __autoreleasing doesn't make any sense. Anywhere within the definition of the pointer type is fine, but once you move onto the pointer-to-pointer type, then you're referring to something else, and it no longer makes sense.

憧憬巴黎街头的黎明 2025-01-03 19:35:09

如果有的话,会有区别

 __weak NSObject *someWeakThing, *someSupposedlyButNotReallyWeakThing;

,因为 __weak 只会令人困惑地应用于第一个变量。 (这与新秀的错误类似,

NSObject* one, two;

当然也不会按“预期”工作)。

There is a difference if you have

 __weak NSObject *someWeakThing, *someSupposedlyButNotReallyWeakThing;

because the __weak will only confusingly apply to the first variable. (this is a similar mistake to the rookie

NSObject* one, two;

which of course won't work as "expected", either).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文