阻止自动保留,是否会影响 self 中的 ivars?

发布于 2024-11-03 19:31:37 字数 261 浏览 1 评论 0原文

如果我有 class:

@interface A : NSObject
{
    BOOL b;
    id   c;
}
@end

并在块中引用 bc ,该块是否会自动保留 self ?或者只是 bc?对于c,它可能会被保留,但是对于b呢?

If I have class:

@interface A : NSObject
{
    BOOL b;
    id   c;
}
@end

and reference b and c in a block, is the block retain self automatically? Or just b and c? About c, it may be retained itself, but how about b?

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

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

发布评论

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

评论(2

荭秂 2024-11-10 19:31:37

Bill 的答案并不完全正确:

如果您有一个 A 实例并在该实例内创建一个块,如下所示:

^{
  b = YES;
}

然后 self 被保留(当复制该块时) )。 b 不是 const 复制的,因为 bself 强引用,并且只有 self 在块的范围内是 const

另一方面,如果您这样做:

BOOL aBool = YES;
^{
  aBool = NO;
  c = [[NSObject alloc] init];
}

那么,self 会被 const 复制(并在复制块本身时保留)并赋值给 c< /code> 是允许的。但是,不允许对 aBOOL 进行赋值,因为 aBool 的值是 const 复制的。

换句话说,编译器将 bc 识别为 ivars,并将保留 self 而不是直接保留 ivars。


思考这个问题的一种方法可以帮助我理解正在发生的事情,那就是记住对象实际上只是一个奇特的结构,这意味着您可以在技术上通过箭头运算符访问 ivars: ->

所以当您正在访问 ivars:

b = YES;

等价于

self->b = YES;

从这个角度来看,您必须保留 self 的原因完全有道理,但是 b 不是 const。这是因为 b 只是“大局”的一小部分,并且为了获得 b,您必须包含所有 self同样(因为复制结构体的一部分在这种情况下并没有真正意义)。

Bill's answer isn't quite correct:

If you have an instance of A and create a block inside of that instance like so:

^{
  b = YES;
}

Then self is retained (when the block is copied). b is not const-copied, because b is strongly referenced by self, and only self is const within the scope of the block.

On the other hand, if you do:

BOOL aBool = YES;
^{
  aBool = NO;
  c = [[NSObject alloc] init];
}

Then again, self is const-copied (and retained when the block itself is copied) and the assignment to c is allowed. However, the assignment to aBOOL is not allowed, because the value of aBool is const-copied.

In other words, the compiler recognizes the b and c are ivars, and will retain self instead of the ivars directly.


One way to think about this that helps me understand what's going on is to remember that an object is really just a fancy struct, which means you can technically access ivars via the arrow operator: ->

So when you're accessing ivars:

b = YES;

is equivalent to:

self->b = YES;

In that light, it makes perfect sense why you have to retain self, but b is not const. It's because b is only a slim part of the "bigger picture", and in order to get b, you must necessarily include all of self as well (since copying part of a struct doesn't really make sense in this context).

め七分饶幸 2024-11-10 19:31:37

块中的代码将有助于回答,但假设您有类似的东西;

^(A *a) {
    [a doSomething];
}

a 会被该块保留,并在该块被释放时被释放。 bc 不会发生任何异常情况。

如果你有类似的东西;

^(id c, BOOL b) {
    [c doSomethingElse];
}

然后,c 将被块保留,而 b 将被块捕获为 const 值(即,您将收到编译器错误:执行b = NO

有关更多详细信息,请参阅文档;

http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html%23//apple_ref/doc/uid/TP40007502-CH6-SW1

Code from the block would be helpful in answering but assuming you have something like this;

^(A *a) {
    [a doSomething];
}

a will be retained by the block and released when the block is released. Nothing outside the ordinary will happen with b and c.

If you have something like;

^(id c, BOOL b) {
    [c doSomethingElse];
}

Then c will be retained by the block and b will be captured by the block as a const value (i.e. you would get a compiler error for doing b = NO)

For more details see the docs;

http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html%23//apple_ref/doc/uid/TP40007502-CH6-SW1

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