ios – 引用块内的实例变量

ios – 引用块内的实例变量,第1张

概述假设我有一个类(非ARC环境): @interface SomeObject : NSObject { UILabel *someLabel; dispatch_queue_t queue;}- (void)doAsyncStuff;- (void)doAnimation;@end@implementation SomeObject- (id)init { 假设我有一个类(非ARC环境):
@interface SomeObject : NSObject {    UILabel *someLabel;    dispatch_queue_t queue;}- (voID)doAsyncStuff;- (voID)doAnimation;@end@implementation SomeObject- (ID)init {    self = [super init];    if (self) {        someLabel = [[UILabel alloc] init];        someLabel.text = @"Just inited";        queue = dispatch_queue_create("com.me.myqueue",disPATCH_QUEUE_SERIAL);    }    return self;}- (voID)doAsyncStuff {    dispatch_async(queue,^{        ...        // Do some stuff on the current thread,might take a while        ...        dispatch_async(dispatch_get_main_queue(),^{            someLabel.text = [text stringByAppendingString:@" in block"];            [self doAnimation];        }    }}- (voID)doAnimation {    ...    // Does some animation in the UI    ...}- (voID)dealloc {    if (queue) {        dispatch_release(queue);    }    [someLabel release];    [super dealloc];}

如果我的块被启动,然后其他所有持有对该对象实例的引用的东西都会释放它,我保证不会调用dealloc,因为嵌套块引用了一个实例变量(和自己) – dealloc嵌套块退出后会发生什么?我的理解是我的街区有很强的自我参考,所以这应该是犹太人.

解决方法 这很好,因为你说的原因.

需要注意的重要一点是,如果类(由self表示)以任何方式保留块,您将创建一个保留周期.因为你是在线定义它,并将它传递给dispatch_async,你应该没问题.

总结

以上是内存溢出为你收集整理的ios – 引用块内的实例变量全部内容,希望文章能够帮你解决ios – 引用块内的实例变量所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1113662.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存