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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:36:25+00:00 2026-05-27T21:36:25+00:00

I thought I had understand the difference between retain and copy . But when

  • 0

I thought I had understand the difference between retain and copy. But when I met the code below I became confused again. These are the code:

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    NSMutableString *a;
    NSMutableString *b;
    NSMutableString *c;
}

@property (nonatomic, copy) NSMutableString *a;
@property (nonatomic, copy) NSMutableString *b;
@property (nonatomic, copy) NSMutableString *c;

@end

ViewController.m:

#import "ViewController.h"

@implementation ViewController

@synthesize a, b, c;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.a = [[NSMutableString alloc] initWithFormat:@"%@", @"aaa"];
    NSLog(@"a:%d", a.retainCount);

    self.b = a;
    NSLog(@"a:%d", a.retainCount);
    NSLog(@"b:%d", b.retainCount);

    self.c = b;
    NSLog(@"a:%d", a.retainCount);
    NSLog(@"b:%d", b.retainCount);
    NSLog(@"b:%d", c.retainCount);
}

@end

can anybody explain why the result is:

2011-12-31 16:54:50.244 RetainCopy[5783:207] a:1
2011-12-31 16:54:50.246 RetainCopy[5783:207] a:2
2011-12-31 16:54:50.246 RetainCopy[5783:207] b:2
2011-12-31 16:54:50.247 RetainCopy[5783:207] a:3
2011-12-31 16:54:50.247 RetainCopy[5783:207] b:3
2011-12-31 16:54:50.248 RetainCopy[5783:207] b:3

? Thanks.

  • 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-27T21:36:25+00:00Added an answer on May 27, 2026 at 9:36 pm

    First, -retainCount is useless to you.

    Second, one misunderstanding is in the implementation of the copy property implementation. You’re not getting a copy here as you would expect. Instead, you are getting the result of [arg copy] rather than [arg mutableCopy]. Many immutable types ‘copy’ themselves like so (simplified):

    - (id)copyWithZone:(NSZone*)zone
    {
      return [self retain];
    }
    

    so the result would ultimately be:

    NSMutableString * a = [[NSMutableString alloc] initWithString:@"aaa"];
    NSString * b = [a copy]; // << returns an immutable copy!
    NSString * c = [b copy]; // << immutable to immutable probably just retains itself!
    

    Since they are immutable and pure, this is semantically sound. It also means that you will need to implement copy accessors for mutable types yourself if the type makes a distinction between an immutable and a mutable copy. Otherwise, your program could blow up when you attempt to mutate the ivar because you are holding an immutable instance. If you add this to the end of -viewDidLoad, you should see an exception: [self.c appendString:@"uh-oh"];

    More detail on implementing a mutable property here.

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

Sidebar

Related Questions

I thought I had a pretty good handle on memory management for objective-c, but
I thought I had this figured out but it turns out I'm just deleting
Alright, I thought I had this whole setTimeout thing perfect but I seem to
I had thought I could enumerate the types using IMetaDataImport.EnumTypeDefs and for each of
I thought I had seen a bug report about this on the jQuery site,
This may seem like a programming 101 question and I had thought I knew
Generally, I had thought it was always better to store images in the filesystem
can anyone help, i have problem doing a sort, I thought i had it
I had a wild thought today. I've been discovering that my users like my
Yesterday i had a question in an interview which i thought i could find

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.