Elliot
Elliot

Reputation: 6136

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

Upvotes: 1

Views: 753

Answers (1)

Joshua Weinberg
Joshua Weinberg

Reputation: 28688

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

Upvotes: 1

Related Questions