简体   繁体   中英

Obj-C ARC: How to remove an object from an array/set and then return it as an autoreleased object?

How do I rewrite this method for ARC?

- (KTThumbView *)dequeueReusableThumbView
{
    KTThumbView *thumbView = [reusableThumbViews_ anyObject];
    if (thumbView != nil) {
        // The only object retaining the view is the
        // reusableThumbViews set, so we retain/autorelease
        // it before returning it so that it's not immediately
        // deallocated when removed form the set.
        [[thumbView retain] autorelease];
        [reusableThumbViews_ removeObject:thumbView];
    }
    return thumbView;
}

The automatic ARC migrator gives me this error:

[rewriter] it is not safe to remove an unused 'autorelease' message; its receiver may be destroyed immediately

Just remove the [[thumbView retain] autorelease]; line. The first line will make a strong reference guaranteeing its around as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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