Amazon.com Widgets Program 17.5
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 18, 2013, 10:26:48 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
| |-+  Chapter Study
| | |-+  Chapter 17 - Memory Management
| | | |-+  Program 17.5
Pages: [1]   Go Down
Print
Author Topic: Program 17.5  (Read 1277 times)
jonthornham
Full Member
***
Posts: 169



WWW Email




« on: September 23, 2009, 10:58:21 PM »

I think I am missing something simple here but I can't see what it is. Let me explain.

Program 17.5 declares an NSString object in the @interface section which is a constant as described earlier. Also we daw a few pages back that you can't autorelease or retain a string constant. However in the @implementation section it is marked for autorelease and retained. Why?

Then in main an NSString object is declared but the alloc message is sent to NSMutableString. Is this allowed?

Code: (Objective-C)
NSString *myString = [NSMutableString stringWithString: @"A string"];

This would all make sense to me is the string in the @interface section and in main were NSMutableStrings like so:

Code: (Objective-C)
@interface ClassA: NSObject
{
    NSMutableString *str;
}

and

NSString *myString = [NSMutableString stringWithString: @"A string"];


Thanks,

Jon



Logged

Jon Thornham
rgronlie
Global Moderator
Full Member
*****
Posts: 212







« Reply #1 on: September 24, 2009, 12:27:53 AM »

Hi Jon,

Don't confuse NSString which is immutable (can't be modified) with a constant string (i.e. @"A string").
An NSString object can be autoreleased or retained if the object it points to is not a constant string.

For example, this will give you an autorelease / retainable NSString:
Code: (Objective-C)
NSString  *myStr = [NSString stringWithCString:"A string" encoding:NSMacOSRomanStringEncoding];


Quote
Then in main an NSString object is declared but the alloc message is sent to NSMutableString. Is this allowed?
Sure Smiley

With the code in the text:
Code: (Objective-C)
NSString *myString = [NSMutableString stringWithString: @"A string"];

If you look up the class method stringWithString you'll see it returns the id data type.
Being a generic data type, it can be assigned to any object type. In this case NSString. Since NSMutableStrings must be mutable (modifiable) the constant string is copied instead of assigned as would happen if you called [NSString stringWithString:@"A string"].

***** Extra detail - This may be confusing - Sorry *****
The result is an NSMutableString object that is statically typed as an NSString. Since NSMutableString is a child class of NSString all the methods for an NSString object work fine.

If you were to try call a method for an NSMutableString object on myStr the compiler would give you a warning that it may not respond since it is statically typed as an NSString. At runtime the method can be called successfully since the object was created as an NSMutableString. Statically typing it doesn't change that fact.

Try calling [myStr appendString:@" appended"]; and see what happens to myStr.

Ryan
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
jonthornham
Full Member
***
Posts: 169



WWW Email




« Reply #2 on: September 27, 2009, 01:50:58 PM »

Thanks Ryan
Logged

Jon Thornham
jonthornham
Full Member
***
Posts: 169



WWW Email




« Reply #3 on: October 08, 2009, 08:19:56 AM »

Oh now I get it!  Grin Thanks for the patience.

Jon
Logged

Jon Thornham
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #4 on: October 08, 2009, 11:31:40 AM »

In Programs 17.4 and 17.5 I probably should change myStr to be an NSMutableString object pointer, as was done in Program 17.3.   That would remove some of the added confusion. 

I'm glad you're going through all these examples so thoroughly!

Cheers,

Steve Kochan
Logged
Soonryidono
Newbie
*
Posts: 4

168544312 ryyvrfrrrrwr@gmail.com
WWW




« Reply #5 on: June 24, 2012, 12:53:54 PM »

Hey guys.
Im looking for a program that will type out the stats of my items for me, so I dont have to do so manually for JSP etc.

So when selling rare items etc, I can copy and paste the item stats straight to JSP..
Anyone know of one I can use with D2NT?
Or just a generic one Not a RV plugin
Thanks
Ï.ñ. íå ñî÷òèòå çà îôôòîï,íî ÿ âîò õî÷ó ñåáå çäåñü êàéò çàêàçàòü... íèêòî â ëè÷êó íå ïîñîâåòóåò?
Logged
Learner
Newbie
*
Posts: 21


Email




« Reply #6 on: July 30, 2012, 10:10:09 AM »

Going off of what the OP asked, I am reviewing 17.5 and I noticed this paragraph of text:

Quote
Suppose that myStr were a mutable string instead of an immutable one, and further suppose that one or more characters in myStr would also affect the string referenced by the instance variable because they reference the same object.

Code: (Objective-C)
NSString *myStr = [NSMutableString stringWithString: @"A string"];

myStr isn't a mutable string? Aren't we allocating/initializing it as one?
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.