Sam
Sam

Reputation: 621

Multiple NSString declaration and initialization

I am new to the ios development.I was checking the NSString and where I had found out that it was excessed using the multiple ways which is as under

1) NSString *theString = @"hello";

2) NSString *string1 = [NSString stringWithString:@"This is"];

3) NSString *string =[[NSString alloc]init]; string =@"Hello";

Now I am confused with above three and would like to know the differences between then?

Upvotes: 3

Views: 528

Answers (3)

Suryakant Sharma
Suryakant Sharma

Reputation: 3960

Yes all three are ways to create string...I try to explain you one by one.

One think you must remember that in Objective-c string is represented by @"". whatever comes double quotes is string eg @"Hello" is string. @"" is basically literal to create NSString.

  1. NSString *theString = @"hello";

EDIT:

In this statement we are creating an NSString constant. Objective-C string constant is created at compile time and exists throughout your program’s execution.

2. NSString *string1 = [NSString stringWithString:@"This is"];

In this statement again, we are creating an autorelease object of NSString, but here a slide diff. than the first case. here we are using another NSString object to create a new autorelease object of NSString. So generally we use stringWithString method when we already have NSString Object and we want another NSString object with similar content.

3. NSString *string =[[NSString alloc]init];
             string =@"Hello";

Here, In first statement you are creating a NSString Object that you owned and it is your responsibility to release it (In non-ARC code), once you done with this object. Second statement is similar to case 1, by string literal you are creating string Object. but when you put these two statements together it causes you memory-leak(In non-ARC code), because in statement one you are allocating & initiating memory for new string object and right after in second statement you are again assigning a new string object to the same string reference.

Upvotes: 2

larva
larva

Reputation: 5148

1) and 2) is the same. there is no difference. [NSString stringWithString: str] just does nothing and returns str if str is immutable already. via this link

3) you create empty NSString pointer. after you assign this string with ' = ' is same way with 1) and it alloc memory to your NSString. You can watch this NSString pointer in 3) and you see pointer was changed:

NSString *string =[[NSString alloc]init];
printf("\n string_1-> = %p\n", string );
string = @"Hello";
printf("\n string_2-> = %p\n", string );

Upvotes: 1

Fogmeister
Fogmeister

Reputation: 77631

OK, first off, the syntax @"blah" is an Objective-C literal to create an NSString. Essentially it is an NSString.

Also, there are two parts to each statement. The declaration of the string variable NSString *string and the instantiation string = <something>.

So...

  1. This is how you would do it most of the time. You declare the variable and set it using the objective-c literal.

  2. This adds a bit of redundancy and you wouldn't really use it like this. You'd maybe use it with a substring or something from another string. As it is you are essentially creating two objects here. The first is [NSString stringWithString:<>]; and the second is @"This is".

  3. This is a bit odd. You are first creating an empty string @"" and then you are replacing this string with @"Hello". Don't do this. There is no point. You may as well just do number 1 as the result is the same.

All three will work but in these exact cases you'd be better off using the first.

Upvotes: 0

Related Questions