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

  • Home
  • SEARCH
  • 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 6925269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:46:04+00:00 2026-05-27T10:46:04+00:00

I have a variable in a class, NSNumber . I want to pass the

  • 0

I have a variable in a class, NSNumber. I want to pass the value of this var to another class var. The problem is that I release the object of the first class and obtain an error message when I try to set the value of the second class var.

In C++ this is so easy to do. But here with memory management and pointers confused me so much.

Solution code, for testing:

#import <Foundation/Foundation.h>
@interface A : NSObject 
{
    NSNumber *a;
}

@property (nonatomic, retain) NSNumber *a;

@end

int main(int argc, char *argv[]) 
{
    NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];

    A *instance1 = [[A alloc] init];
    A *instance2 = [[A alloc] init];

    [instance1 setA:[NSNumber numberWithFloat:5.43f]];

    instance2.a = [instance1.a copy];
    [instance1 release];

    NSLog(@"Valor de la que sigue viva, parte2: %@", instance2.a);


    [instance2 release];
    [p release];
    [pool drain];

    return 0;
}
  • 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-27T10:46:05+00:00Added an answer on May 27, 2026 at 10:46 am

    You should use a retain property or copy the instance variable:

    @interface A {
     NSNumber *a;
    }
    
    @property (nonatomic, retain) NSNumber *a;
    @end
    
    ...
    A *instance1 = [[A alloc] init];
    A *instance2 = [[A alloc] init];
    
    instance1.a = instance2.a;
    //or
    instance2.a = [instance1.a copy];
    

    Read some docs about retain-counted memory management which is what Objective-C uses.

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

Sidebar

Related Questions

Suppose that I have an integer variable in a class, and this variable may
I have a private class variable that holds a collection of order items: Private
I have a variable declared like this in a class: Entity *array[BOARD_SIZE][BOARD_SIZE]; I need
I have a static variable partner in the class. And I want to set
I have a string variable that represents the name of a custom class. Example:
I have a global variable that is an instance of my custom class. How
I want to have a string variable for a PHP class, which would be
I have a class variable that I would like to set from an initilizer
I have a class that works by summing NSNumbers. One particular use of this
I have declared a class variable in here void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {

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.