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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:22:40+00:00 2026-05-26T14:22:40+00:00

I see many discussions saying that I should use copy for NSString property because

  • 0

I see many discussions saying that I should use copy for NSString property because it will prevent others from changing it behind my back. But then why don’t we just set readonly property for it?

Update

Thanks for answering my question. But the thing is that for NSString property, you always don’t want others to modify it, right? You may modify it yourself but definitely not others. I guess most of time NSString get its initial value set up (either by you or by others), after that only you will modify it. Then why not just use readonly property

Actually I use copy most of time. But then I realize most of time I only use those setters in my init method. So I think I should use readonly instead of copy for those case.

So let me ask question in this way: if you only use those setters for your NSStrings in your init method, then you should use readonly instead. Is this a reasonable conclusion ?

  • 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-26T14:22:41+00:00Added an answer on May 26, 2026 at 2:22 pm

    if you only use those setters for your NSStrings in your init method, then you should use readonly instead. Is this a reasonable conclusion?

    Since you should not use accessors in partially constructed states (init/dealloc), then you should declare it as copy and readonly, then perform the copy in the initializer:

    - (id)initWithName:(NSString *)inName
    {
      self = [super init];
      if (0 != self) {
        name = [inName copy];
      }
      return self;
    }
    

    In more detail, copy and readonly are semantically different concepts.

    • You use copy because you are interested in the value in most cases. It’s also a safeguard and an optimization to use immutable strings.

    • You use readonly to prohibit clients from mutating/setting your data.

    Together, they provide a good degree of safety, but alone:

    • copy still allows clients to set the value at any point in the program’s execution via the setter.

    • readonly does not imply copy, and a retained property could be changed behind your back; consider what happens when you are passed a mutable variant and the client mutates it after calling the setter.

    The safest way is to use copy and readonly.

    • obviously, you will use readwrite when you need to provide a setter to your clients, and you support that change.

    • retaining a string (or array, or…) instead of copying is usually a bad idea. there is rarely a good use for you not to copy these types, and it can lead to subtle errors. even when you are dealing with a mutable type, you’ll usually want a mutable copy (which the compiler will not synthesize for you). retaining or assigning these types is almost never what you want. one exception i make is when dealing with large allocations, where the data is encapsulated well (e.g. a heavy NSMutableData instance which I pass ownership from one place to another to avoid a copy).

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

Sidebar

Related Questions

I see many, many sites that have URLs for individual pages such as http://www.mysite.com/articles/this-is-article-1
I see many methods that specify an interface as return value. Is my thought
Sometime I see many application such as msn, windows media player etc that are
I see many people coming to my facebook game with HTTP referer that includes
I see many CS students who use Dropbox as an alternative to real SC
I see many applications that uses node runs forever. Therefore, I tried using setInterval
I see many user interface control libraries for .NET, but where can I get
I see many similar questions, however I want to find the Username of the
I see many questions and answers about using C# to generate PDF files. I
If you make a little research on the internet you'll see many DLLImport styles

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.