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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:59:16+00:00 2026-05-26T22:59:16+00:00

I recently rewrote some code in one of my classes, which gave me an

  • 0

I recently rewrote some code in one of my classes, which gave me an error with an NSString. Here is what I have now:

My class header:

@interface MyViewController : UITableViewController {
   NSString *myString;
}

@property (nonatomic, retain) NSString *myString; // Or copy instead of retain.

@end

And implemented some methods:

- (void)viewDidLoad {
    myString = @"This is";

    if (something) {
        myString = [NSString stringWithFormat:@"%@ a string.", myString]; // *1
    }

    [myString retain]; // <-- Why do I have to retain/copy here?
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        /* Some code for creating a UITextView called myTextView */
        // ..and then setting the text property:
        myTextView.text = myString; // <-- Crashes here if I don't retain/copy in viewDidLoad.
    }
}

After some debugging I figured I had to retain/copy the NSString.

Why do I have to retain/copy the NSString in viewDidLoad if I want to use it later on?

Also, I noticed that if I remove the line marked *1, I don’t have to retain/copy.

  • 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-26T22:59:16+00:00Added an answer on May 26, 2026 at 10:59 pm

    If you use the synthesized setter method to assign the variable, it will automatically retain the NSString for you:

    if (something) {
        self.myString = [NSString stringWithFormat:@"%@ a string.", myString]; // *1
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently, I was writing a class in which I discovered that I could reduce
Recently I have released my app into the US AppStore. Now I Planed for
I have been running into some troubles recently and I think I need your
I have recently bought an Arduino Uno , and now I am experimenting a
I have recently rewrote my application and started with a fresh project. The App
Recently something changed on my server, which has been causing an internal error 500
I've recently had some quite strange race condition in the applet code. Looks like
I have a C# service application which interacts with a database. It was recently
I have recently changed my site permalink. Some links are indexed by google and
Recently one of our app servers went down, when it was rebooted the Python

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.