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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:46:48+00:00 2026-05-22T18:46:48+00:00

I am implementing a copyWithZone method for a custom A class, in which a

  • 0

I am implementing a copyWithZone method for a custom A class, in which a NSNumber pointer was declared as (retain) property

@class A <NSCopying>
{
  NSNumber *num;
}
@property (nonatomic, retain) NSNumber *num; // synthesized in .m file

-(id) copyWithZone:(NSZone*) zone {

   A *new = [[A alloc] init];
   new.num = [num copy];
   return new;
}

When I debug, I always find new.num is the same address as the self.num.

Even if I use

new.num = [NSNumber numberWithFloat: [num floatValue]];

I still get the same address. In the end, I have to use

new.num = [[[NSNumber alloc] initWithFloat:[num floatValue]] autorelease]

to achieve the result I want. I am just wondering why NSNumber complies to but does not return a new memory address when copied?

Thanks

Leo

  • 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-22T18:46:49+00:00Added an answer on May 22, 2026 at 6:46 pm

    NSNumber is immutable. Making a copy is pointless and, thus, the frameworks just return self when copy is invoked.

    If a class implements NSCopying, you should mark the property as copy (not retain). -copy on immutable classes (NSString) will simply return a reference to the object (w/a bumped retain count). If passed a mutable instance, it’ll be copied to an immutable instance. This prevents an external party from changing the state behind your object’s back.

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

Sidebar

Related Questions

I have a small class hierarchy that I'm having trouble implementing copyWithZone: for. I've
I might be missing something obvious here, but I'm implementing NSCopying on one of
Implementing a custom VM and I've come to use registers (these will store pointers
When implementing a class with multiple properties (like in the toy example below), what
I implementing OLE DB provider for my custom database. It will be used from
Implementing custom DataAnnotationsModelMetadataProvider in ASP.NET MVC2. Assuming the object that is being rendered looks
I`m implementing a custom filesystem on Ubuntu using Fuse, but I need to trap
Implementing Interface just provide the skeleton of the method. If we know the exact
Implementing Equals() for reference types is harder than it seems. My current canonical implementation
Implementing a 'sandbox' environment in Python used to be done with the rexec module

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.