Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-10-2011, 09:33 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 1
zv77rus@gmail.com is on a distinguished road
Default NSString

so if I want to create an object NSString, I do the following:

NSString *newText = [[NSString alloc] initWithFormat:@"%d", someNum]

I assume, the object is allocated on the heap. So if I want to delete it, I will do the following:

[newText release];


But what if I use the following code:
NSString *newText = [NSString stringWithFormat: @"%d", someNum];

Is it allocated in the stack? is [NSString stringWithFormat: @"%d", someNum] created locally and will be automatically deleted when out of scope? Why is the following line gives me an error when I am trying to delete the object in this case created in the previous example?

[newText release];


Please, help. Thanks!
zv77rus@gmail.com is offline   Reply With Quote
Old 07-11-2011, 04:26 PM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

The first one creates an NSString object with a retain count of 1. Therefor you have release it when you are done. The second one creates an autoreleased object. If you want to keep it around, you retain it, if not let it be. It'll be sent a release message later on and it'll be deallocated.
Domele is offline   Reply With Quote
Old 01-04-2012, 09:22 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 2
Heartbound is on a distinguished road
Default

Quote:
Originally Posted by zv77rus@gmail.com View Post
so if I want to create an object NSString, I do the following:

NSString *newText = [[NSString alloc] initWithFormat:@"%d", someNum]

I assume, the object is allocated on the heap. So if I want to delete it, I will do the following:

[newText release];


But what if I use the following code:
NSString *newText = [NSString stringWithFormat: @"%d", someNum];

Is it allocated in the stack? is [NSString stringWithFormat: @"%d", someNum] created locally and will be automatically deleted when out of scope? Why is the following line gives me an error when I am trying to delete the object in this case created in the previous example?

[newText release];


Please, help. Thanks!

Becuase when you stored the object in the 'newText' pointer, you retained it by using 'alloc'. That means you're responsible for it's release.

In the second Line of your code, you used a Class method that did almost the same thing but what you don't see is that it also does a 'autorelease' on your object and as such it is released and you do not need to release it. Doing so will result in a crash.

The second line is the equivalent to 'NSString *newText = [[[NSString alloc]initWithWhatever...]autorelease];'

it does all that in the 'stringWithFormat' method that works on the NSString class. Obviously the 'stringWithFormat' is a class method.
Heartbound is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 474
16 members and 458 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, HemiMG, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:15 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0