Amazon.com Widgets page 347 NSString help
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 23, 2014, 09:23:31 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Program Examples
| | |-+ Chapter 15
| | | |-+ page 347 NSString help
Pages: [1] Go Down
Print
Author Topic: page 347 NSString help (Read 1883 times)
delgreco
Newbie
*
Posts: 7


Email




on: February 20, 2010, 03:51:03 PM

Ok, i'm hoping someone out there can help me understand something:

On page 346 (333 3rd edition) in program example 15.8, it shows the following method for setName:

name = [[NSString alloc] initWithString: theName];

On page 347, it says that if it was done as name = [NSString stringWithString: theName]; that this would be incorrect because the object would still not own its member objects.....but I don't understand why??  probably because i don't understand the difference between these 2 methods....

I think i understand the first one.....obviously i am allocating it myself with the alloc method.  When you look up the initWithString method definition, it says it sets a newly allocated string to NSString.  So, i allocated and the initWithString initialized.

Now for the second method, the definition for stringWithString says it creates a new string, setting it to NSString.  So does that not mean that this method, stringWithString, allocates and initializes the name instance variable?Huh  which would be what i did in the first method, correct??

Again...why is one way acceptable and the other is not.....as i was under the impression that both methods would allocate and initialize?Huh  Please help, thanks guys.
Last Edit: August 10, 2011, 10:27:53 AM by fujilla Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: February 20, 2010, 08:35:37 PM

I think I could have done a better job with the phrasing there.  When you alloc your own object you own it in the sense that you can decide when you want to release it, which is when you're done using it.  Using a  method like stringWithString: (that is any Foundation method that creates an object without alloc, copy, or new), the object is added to the autorelease pool by the method, so you don't have the ability to release it when you're done using it.   You have to wait for the autorelease pool to be drained.  When you're running an iPhone app, that won't happen until you're finished processing an event.

Cheers,

Steve Kochan
Logged
delgreco
Newbie
*
Posts: 7


Email




Reply #2 on: February 20, 2010, 09:05:33 PM

Steve, thank you for the reply.

I have one more question......I understand now from what you are telling me that in the first method using alloc, I created the object so I also have to release it when I am done using it.  I also understand now that in the stringWithString method, this also allocates and initializes, but this type of object is added to the autorelease pool.

But I still don't understand why only the first method is acceptable and allows the object to own its member object, whereas the second method would be incorrect??  Could you please help, thanks Steve.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: February 21, 2010, 08:59:21 AM

Well, it's not acceptable because if the autorelease pool gets drained, your name instance variable could disappear without the object's owner  knowing it---even if the object is retained (explained in the chapter on memory management).   Also, if you write a method that is executed many times, you can end up accumulating objects in the pool that won't be released until the pool itself is drained.  With this code:

Code: (Objective-C)
if (name) 
   [name release];

name = [[NSString alloc] initWithString: theName];

you release the old object before allocating the new one, which is a more efficient approach.  You couldn't do this if the object was not owned by you and had been added to the autorelease pool by the stringWithString: method.

Cheers,

Steve Kochan
Logged
delgreco
Newbie
*
Posts: 7


Email




Reply #4 on: February 22, 2010, 05:42:51 PM

I think I got it now......thanks Steve, much appreciated!!
Logged
Fitoepist
Newbie
*
Posts: 1

[url="http://proxy.my-addr.com"]web proxy online[/url]

384652728 insoleyurizoh@gmail.com




Reply #5 on: April 15, 2011, 01:14:55 PM

Okai thanks, but wouldnt it get saved over the other index, the main page for website?
Logged

Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.