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

The Archive Base Latest Questions

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

I just wrote some code. It has my own custom made class. In that

  • 0

I just wrote some code. It has my own custom made class. In that custom made class called ‘WonderfulNumbers’ I have a method that looks like this

- (NSString *)storedNumberAsString {
NSString *stringToReturn = [[NSString alloc]initWithFormat:@"Hello World"];
return [stringToReturn autorelease];
}

I obviously then #import “WonderfulNumbers” into the main and etc
then in my main I have an IBAction.
it reads like the following

-(IBAction)displaySomeText:(id)sender {
    WonderfulNumbers *myNumber = [[WonderfulNumbers  alloc]init];// Step 1
    NSString *numberString = [myNumber storedNumberAsString];// Step 2
    [textView insertText:numberString];// Step 3
    //textView is a variable  of NSTextView.
    [myNumber release];// Step 4
}

I get the step by step process, and the logic behind this.
What I like to know and try to reassure to my self is what exactly is happening at step 2.

I understand Step 1, 3 and 4.

But step 2, I crated an NSString variable called ‘numberString’ and it obviously holds the object called ‘myNumber’ with the method described above applied to it. Makes sense.

What I want to know is, I never allocated or initialized the ‘numberString’ object of NSString class. How am I able to then use it.
I know I don’t release it since it’s never allocated .. but did I initialize it by just doing [myNumber storedNumberAsString]; ?

A slight explanation would be awesome. Thank’s to those that reply.

P.S. I know that everything in objective-c is an object but just for the sake of this argument, since ‘numberString’ is not technically “created by allocate and init” is it right to call that a variable?

I think I know the differences between the two but just want reassurance.

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-26T15:22:09+00:00Added an answer on May 26, 2026 at 3:22 pm

    You are initializing NSString *stringToReturn to the return value of [myNumber storedNumberAsString]. Inside storedNumberAsString you can see that it returns an NSString reference, properly allocated and all, so it’s fine to use.

    The key here is autorelease, which causes the object to be released automatically “sometime later” (actually when the topmost autorelease pool is released, which, unless you changed it yourself, tends to happen after each iteration of the event loop).

    The convention in Objective-C is that if you alloc or retain an object, or get an object from a method whose name begins with new, copy, or mutableCopy, you are responsible for releasing it. Otherwise you can assume it will be released by someone else (e.g., later via autorelease). Since storedNumberAsString does not begin with new, copy, or mutableCopy, you don’t need to release it. Meanwhile the implementation of storedNumberAsString must ensure that the object it allocs gets released — in this case that is done by calling autorelease on it.

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

Sidebar

Related Questions

I just wrote some code to test the behavior of std::equal, and came away
I just wrote some new utility methods within a non-page class for an existing
I have some Javascript code that needs to be dynamically created as a String
I wrote some code to tackle a work-related problem. The idea is that the
Hey i'm just trying to write some code in DrScheme: ((function (x) (* x
I just wrote my first web service so lets make the assumption that my
So I'm working with some older code I wrote some time ago. I was
The code I'm working with has its own smart pointer implementation which does simple
My application has a plugin system that allows my users to write their own
I wrote a program in Java 6.0, but it turns out that some of

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.