Re: literal strings - who do they belong to?
Re: literal strings - who do they belong to?
- Subject: Re: literal strings - who do they belong to?
- From: Ben Cox <email@hidden>
- Date: Sun, 12 Jul 2009 11:23:12 -0400
NSString +stringByAppendingString:(NSString*) returns an autoreleased
string. If you release it explicitly, your application will crash.
-- Ben
On Sun, Jul 12, 2009 at 11:16 AM, William Squires <email@hidden>wrote:
> According to Cocoa/ObjC memory-management, if you get an object (reference)
> via "alloc","new",any object that contains "copy", and any object you send
> [<object> retain] to, is yours - it becomes your responsibility to call
> [<your object> release] on it when you're done. Ditto for properties like
>
> @property (nonatomic,retain) <data type>myProp;
>
> where "retain" is specified.
>
> But what about literal NSStrings?
>
> I have a method in my view controller class.
>
> - (void)setWebView:(SomeAppDelegate *)appDelegate
> {
> NSString *htmlString;
>
> htmlString = @"<div style=\"font-family;Helvetica,Arial, sans-serif;
> font-size=48pt;\"align=\"center\">";
> htmlString = [htmlString stringByAppendingString:appDelegate.savedNumber];
> htmlString = [htmlString stringByAppendingString:@"</span>"];
> [webView loadHTMLString:htmlString baseURL:nil];
> // [htmlString release]; // <- is this necessary?
> }
>
> For reference, SomeAppDelegate.h declares "savedNumber" as a property like
> the above. i.e.
>
> @interface SomeAppDelegate : NSObject <UIApplicationDelegate>
> {
> NSString *savedNumber;
> }
>
> @property (nonatomic,retain) NSString *savedNumber;
> @end
>
> and is synthesized in SomeAppDelegate.m:
> @implementation SomeAppDelegate
>
> @synthesize savedNumber
> ...
> @end
>
> Does the setWebView: method need an [htmlString retain] somewhere? Does
> [NSString stringByAppendingString] do anything funny I should know about?
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden