differences between "[NSString alloc] initWithString...." and @"..."

2,236 views
Skip to first unread message

iPolly

unread,
May 8, 2009, 3:19:58 PM5/8/09
to iPhone Application Development Auditors
hello,

im new here with iPhone programming. can anyone tell me the
differences between these 2 commands?

1- NSString *s = @"Hello World"
2- NSString *s = [[NSString alloc] initWithString:@"Hello world"];

when should i use the "alloc" one?

many thanks.

Miguel Andrés Yáñez Barreto

unread,
May 8, 2009, 3:27:05 PM5/8/09
to iphone-appd...@googlegroups.com
The first one is a constant, you don't alloc it or release it, because it's in the "data" section of the compiled code.

You shouldn't use the second one with a constant string. The second one is a variable that you alloc, so you would have to release it yourself...



Miguel Andrés Yáñez Barreto - Asistente Graduado
Especialización en Seguridad de la Información
Presidente de Maratones Uniandes

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "iPhone Application Development Auditors" group.
To post to this group, send email to iphone-appd...@googlegroups.com
To unsubscribe from this group, send email to iphone-appdev-aud...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/iphone-appdev-auditors?hl=en
-~----------~----~----~----~------~----~------~--~---


Allen Curtis

unread,
May 8, 2009, 5:24:13 PM5/8/09
to iphone-appd...@googlegroups.com
> im new here with iPhone programming. can anyone tell me the
> differences between these 2 commands?
>
> 1- NSString *s = @"Hello World"

This is a special case which does not need to be released or retained.
It acts like a "C" string.

>
> 2- NSString *s = [[NSString alloc] initWithString:@"Hello world"];

This version is retained and must be explicitly released.

There are other versions like [NSString stringWithFormat:@"%d" number]
This is autoreleased and must be explicitly retained.

>
>
> when should i use the "alloc" one?
>

When you need to construct a string that you know you want to retain.

> many thanks.
> >

Saurabh G

unread,
May 11, 2009, 2:52:21 PM5/11/09
to iPhone Application Development Auditors
Thanks for the reply. Does the same logic work with NSArray? Meaning
that if you use [NSArray arrayWithObjects: @"Triangle", @"Square",
@"Pentagon", @"Hexagon", nil], you don't need to release it since it's
made up of constants?

I did the autorelease routine on such an array, but when I executed
it, the program just seemed to go on an infinite loop. I haven't
gotten as far as using gdb, yet, so I did not look into what went
wrong, and simply commented the autorelease message.

Thanks in advance!

-Saurabh

On May 8, 3:27 pm, Miguel Andrés Yáñez Barreto
<migueland...@gmail.com> wrote:
> The first one is a constant, you don't alloc it or release it, because  
> it's in the "data" section of the compiled code.
>
> You shouldn't use the second one with a constant string. The second  
> one is a variable that you alloc, so you would have to release it  
> yourself...
>
> Miguel Andrés Yáñez Barreto - Asistente Graduado
> Especialización en Seguridad de la Información
> Presidente de Maratones Uniandes
> migueland...@gmail.com
>
> On May 8, 2009, at 14:19 , iPolly wrote:
>
>
>
>
>
> > hello,
>
> > im new here with iPhone programming. can anyone tell me the
> > differences between these 2 commands?
>
> > 1- NSString *s = @"Hello World"
> > 2- NSString *s = [[NSString alloc] initWithString:@"Hello world"];
>
> > when should i use the "alloc" one?
>
> > many thanks.
> > >
>
>
>  smime.p7s
> 3KViewDownload

Allen Curtis

unread,
May 11, 2009, 2:58:17 PM5/11/09
to iphone-appd...@googlegroups.com
The NSArray returned has already been autoreleased before it was
returned. I don't know if the redundant autorelease would through you
into an infinite loop. I would hope not.

bluelobe

unread,
May 11, 2009, 6:47:29 PM5/11/09
to iPhone Application Development Auditors
Here's my understanding. You use...

NSString *s = @"Hello World" and

[NSArray arrayWithObjects: @"Triangle", @"Square",
@"Pentagon", @"Hexagon", nil]

when you know beforehand what content you're going to put in. And you
use...

NSString *s = [[NSString alloc] initWithString:[someClass
returnsStringMethod]];

when you're getting content that you don't know beforehand and that
you'll pass/use for other purposes other than displaying something.
Reply all
Reply to author
Forward
0 new messages