Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6728391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:09:53+00:00 2026-05-26T10:09:53+00:00

I am converting some code to ARC. The code searches for an element in

  • 0

I am converting some code to ARC. The code searches for an element in an NSMutableArray, then finds, removes, and returns that element. The problem is that the element gets deallocated immediately upon “removeObjectAtIndex”:

- (UIView *)viewWithTag:(int)tag
{
    UIView *view = nil;
    for (int i = 0; i < [self count]; i++)
    {
        UIView *aView = [self objectAtIndex:i];
        if (aView.tag == tag) 
        {
            view = aView;
            NSLog(@"%@",view); // 1 (view is good)
            [self removeObjectAtIndex:i];
            break;
        }
    }
    NSLog(@"%@",view); // 2 (view has been deallocated)
    return view;
}

When I run it, I get

*** -[UIView respondsToSelector:]: message sent to deallocated instance 0x87882f0

at the second log statement.

Pre-ARC, I was careful to retain the object before calling removeObjectAtIndex:, and then to autorelease it. How do I tell ARC to do the same thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:09:53+00:00Added an answer on May 26, 2026 at 10:09 am

    Declare the UIView *view reference with the __autoreleasing qualifier, like so:

    - (UIView *)viewWithTag:(int)tag
    {
        __autoreleasing UIView *view;
        __unsafe_unretained UIView *aView;
    
        for (int i = 0; i < [self count]; i++)
        {
            aView = [self objectAtIndex:i];
            if (aView.tag == tag) 
            {
                view = aView;
                //Since you declared "view" as __autoreleasing,
                //the pre-ARC equivalent would be:
                //view = [[aView retain] autorelease];
    
                [self removeObjectAtIndex:i];
                break;
            }
        }
    
        return view;
    }
    

    __autoreleasing will give you exactly what you want because on assignment the new pointee is retained, autoreleased, and then stored into the lvalue.

    See the ARC reference

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some C# code that I'm converting to Objective-C. In C# I would
I am converting some functioning Haskell code that uses Parsec to instead use Attoparsec
I've run into a problem whilst converting some C code to PHP, specifically in
I am converting some code from another language to python. That code reads a
I had some code in a shouldOverrideUrlLoading, that downloads the data, converting if necessary,
I'm converting some code that currently uses an XmlWriter to create a document to
I have some code that I'm converting from Perl to Java. It makes pretty
I am converting some code to Scala. It's code that sits in an inner
I am converting some c code to python. I am wondering that is there
I am converting some Java code to C#. This code is using getGlyphOutline from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.