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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:15:52+00:00 2026-06-10T07:15:52+00:00

In the following example, does stringWithString:(NSString *) copy the memory address/location of theName to

  • 0

In the following example, does stringWithString:(NSString *) copy the memory address/location of theName to name or it actually copies the data from theName to name?

@interface AddressCard:NSObject
-(void)setName:(NSString *)theName;
@end

@implementation AddressCard
NSString *name;
-(void)setName:(NSString *)theName
{
    if(name!=theName)
    name = [NSString stringWithString:theName];
}
@end

If I change the code to following, what does copy do differently?

@interface AddressCard:NSObject
@property (copy, nonatomic) NSString *name;
@end

@implementation AddressCard
@synthesize name;
@end

In general, does copy (@property attribute) copy the address of the data or copies the data from one variable to another? If it is latter case, are we not consuming a lot of memory when the variable represents large data?

Thank you for your time and response!

  • 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-10T07:15:54+00:00Added an answer on June 10, 2026 at 7:15 am

    +[NSString stringWithString:] will effectively ‘copy’ the string.

    In general, does copy (@property attribute) copy the address of the data or copies the data from one variable to another?

    It performs whatever the object considers is a copy. It may return a new object, or it may return itself. For example, +[NSString stringWithString:]could just return the parameter retained and autoreleased if the parameter is already immutable. If the parameter is mutable, then it will return a new instance, so you are guaranteed to have an immutable instance.

    If it is latter case, are we not consuming a lot of memory when the variable represents large data?

    Aha – but that’s the trick! Yes, you could end up making many new allocations with copy, but the trick is often that copies of reference counted objects are truly very shallow in most cases when you favor immutable types and using copy. Many collections types can simply return themselves if they are already immutable, or their ivars may do so, so it’s actually a really good idea to ensure you are not passing around mutable objects — so creating an immutable copy early really allows this optimization to propagate, and saves you a ton of allocations (but not always — there are a number of corner cases for all these variants).

    Note: Not all classes distinguish immutability from mutability, so a copy does not always return an immutable object.

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

Sidebar

Related Questions

From the Scala API , I got the following example, which does not compile;
The following example from boost bind does not work for me: #include <boost/bind.hpp> struct
Why does Location.distanceBetween throw an Exception with the following example: Location.distanceBetween(51.5175431381974,-0.1501073187713473, 51.5173695544231, -0.15127676190184, results);
what does the colon mean in php in the following example. ::1 if($host ==
I found an example for async ftp upload on msdn which does the following
Does anybody know why my getAltitude in the following always returns 0? package com.example.helloandroid;
does any one know if, for example, I'm writing in c++ the following code:
Consider the following example: // does not work foo( func_num_args() ); // works $args
How does the following example usage of extern specifer behave. We have a global
Why does FindName() return null in the following example? XAML: <Window x:Class=TestDynamicTextBox343.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml

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.