[fbcocoa] Re: [fbcocoa] NSRunAlertPanel function

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2010 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Thu, 8 Jul 2010 13:14:06 -0400
Brian wrote concerning string literals vs. NSStrings;

>That was my first reaction too. It's a literal ( which in C is 
>generated by the CFSTR macro ). However, the following snippet from 
>"Learn Objective-C on the Mac" by Dalrymple/Knaster( page 220 in 
>Chapter 12 on Categories ) in reference to this code which 
>essentially added a category to NSString class:
>
>[dict setObject: [@"Hello" lengthAsNumber] forKey: @"hello"];
>
>says: "Remember that the @"string" kind of strings are actually 
>full-blown NSString objects. They react to messages just like any 
>other NSString object"

Brian,

The problem I see with this reasoning is that a string literal does 
not need to be released while an NSString-- existing as a true 
NSObject-- follows the rules of reference counting and must be 
released. That is why this Cocoa method exists:

    myString = [NSString stringWithString:@"Hello"];

It takes a string literal and creates a viable pointer to an NSObject.

Note that in the example above, a mutable NSObject-- a dictionary-- 
is filled with a string literal. However the result remains an 
instance of an NSObject that follows the create/retain/release rule.

Ken