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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:50:39+00:00 2026-05-10T23:50:39+00:00

I understand that any init… method initializes a new object and that NSString stringWithString

  • 0

I understand that any init… method initializes a new object and that NSString stringWithString makes a copy of the parameter string as a new object. I also understand that being the objects’ owner, I can control the release/deallocation of any objects that I allocate. What I don’t understand is when would I use the stringWithString method since any local variable assigned that way would have it’s memory ‘owned’ by NSString instead of the local class.

The ‘Programming in Objective C’ book by Kochan (1st ed) uses the following code (see pages 342-344) to explain that the initWithString is preferable to stringWithString because the AddressCard class would own the name variable contents. Also, I don’t get any errors making repeated calls to the setName version with the stringWithString method. TIA!!

//header file has appropriate declarations but not included here: #import 'AddressCard.h'  @implementation AddressCard;  -(NSString *) name {    return name; }  //Recommended code: -(void) setName: (NSString *) theName {    [name release]    name = [[NSString alloc] initWthString: theName]; }  //Incorrect code according to Kochan: -(void) setName: (NSString *) theName {    [name release]    name = [NSString stringWthString: theName]; }  //rest of class implementation code snipped @end 
  • 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. 2026-05-10T23:50:40+00:00Added an answer on May 10, 2026 at 11:50 pm

    What I don’t understand is when would I use the stringWithString method since any local variable assigned that way would have it’s memory "owned" by NSString instead of the local class.

    That’s not correct.

    Object ownership is determined by five rules, based on the name of the method you get an object from:

    • Any object returned by alloc, copy, copyWithZone, or new has a retain count of 1.
    • retain increases the receiving object’s retain count.
    • release decreases the receiving object’s retain count.
    • autorelease tells the current autorelease pool to send the receiving object the release message “later”.
    • Any factory method that doesn’t have “new” or “copy” in the name (e.g., stringWithString:) returns an object that it has autoreleased on your behalf.

    Or, digested a bit:

    • Any method whose name contains copy, alloc, retain, or new returns an object that you own.
    • Any method that doesn’t, returns an object that you don’t own.
    • To own an object, retain it.

    The incorrect implementation of setName: that you show is incorrect because it stores an autoreleased object in an instance variable, when you mean to own the object. You should retain it or, in this case, copy it. One way is to simply use alloc and initWithString:, as in the correct example you show; the other way would be copy.

    The Memory Management Programming Guide for Cocoa explains everything. Every Cocoa or Cocoa Touch programmer should read or re-read it from time to time.

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

Sidebar

Ask A Question

Stats

  • Questions 101k
  • Answers 101k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Found the answer in a MS KB article: File permissions… May 11, 2026 at 8:02 pm
  • Editorial Team
    Editorial Team added an answer There are two basic options. Images (and pictures of text… May 11, 2026 at 8:02 pm
  • Editorial Team
    Editorial Team added an answer You cannot do a "put" with extends . Look at… May 11, 2026 at 8:02 pm

Related Questions

I understand that any init... method initializes a new object and that NSString stringWithString
Is it at all possible to disable the The webpage you are viewing is
I don't plan to write applications without IB, I'm just in the process of
I'm just starting out (reading up a lot for the past couple of days).
How do I write a Python class that handles calls on undefined methods by

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.