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 9222895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:56:39+00:00 2026-06-18T03:56:39+00:00

If I have a local pointer to a custom class, how can I store

  • 0

If I have a local pointer to a custom class, how can I store its pointer in a class level NSDictionary variable and use it after its initial creation scope has terminated.

How can I tell the compiler to retain the memory created by new until the class I created it in has gone out of scope?

This is sudo code to represent the scoping of the variables in question.

Class {

    NSMutableDictionary* allElements;

    SomeFunction {

       InnerClosure1 {

       DefaultElement* element = [DefaultElement new];
       [allElements setObject:element forKey:@:"Foo"];

       }

       InnerClosure2 {

          DefaultElement* element = [allElements objectForKey:@"Foo"];

      }
}

The problem is I get this error after trying to put the pointer in the dictionary:

[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to
instance 0x94421d0 2013-01-31 23:35:32.575
Muskoka-Domicile[1371:19d03] * Terminating app due to uncaught
exception ‘NSInvalidArgumentException’, reason: ‘-[__NSDictionaryI
setObject:forKey:]: unrecognized selector sent to instance 0x94421d0’

I suspect that it has something to do with transferring ownership of a custom class?

Edit
Turns out initialized the NSMutableDictionary* to a NSDictionary…

  • 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-06-18T03:56:40+00:00Added an answer on June 18, 2026 at 3:56 am

    Your local pointer can be lost. What you’re interested in is the chunk of memory (object) you’re pointing at.

    When you add any object to NSDictionary, that object is retained by the dictionary. You can leave scope1 and your object will be just fine. In fact, if you are not using arc, after adding your element to the dictionary you would typically call [element release] to balance out your call to new.

    Later, when you pull the object out of the dictionary, you are just updating the temporary pointer (element) with the memory address of your object. In ‘Scope2’ Since you did not create the object, you would not release it when done.

    Hope that helps clarify things.

    edit/update
    I should note, your code is basically correct as is. If you want to put objects into a dictionary, you need an NSMutableDictionary, so make one of those at the top of your code. Next, you should call setObject:forKey: not setValue. That should do the trick for you. You may need to add a cast to (DefaultElement *) to suppress a warning when you pull your element out of the dictionary.

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

Sidebar

Related Questions

Possible Duplicate: Pointer to local variable Can a local variable's memory be accessed outside
I have local data in a grid. How can I get all of the
I have local server for my site (i made it with XAMPP). How can
I have a local variable in an erb template: <% thumbnail_width = 50 %>
I have a module that is used by creating a custom class loader. The
I have to store a TList of something that can easily be implemented as
I have a pointer available with me to a C/C++ variable. Is it possible
I have a function in which I make use of a local array. I
I have been trying to create a pointer variable in the called function and
I'm working on a project with couple others. We all have local copies of

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.