3

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?

madLokesh
  • 1,860
  • 23
  • 49
Sam
  • 571
  • 1
  • 6
  • 22
  • @trojanfoe can you elaborate? – Rui Peres Feb 24 '14 at 11:01
  • `string` is created (empty) and then re-initialized with a constant string. It's an inefficient version of 1). – trojanfoe Feb 24 '14 at 11:01
  • 3
    That's a very different explanation than "is broken". Specially when you are talking to a new person on iOS development. – Rui Peres Feb 24 '14 at 11:02
  • Well I hope I've made-up for that short fall. – trojanfoe Feb 24 '14 at 11:03
  • I wouldn't say it's broken at all. He obviously meant to put that and was asking what it does. – Fogmeister Feb 24 '14 at 11:05
  • Check the following. http://stackoverflow.com/questions/13373826/using-stringwithstring-with-a-literal-is-redundant – Shekhu Feb 24 '14 at 11:07
  • @Nexttric I would suggest you follow hands on apprentice series of [Ray Wenderlich](http://www.raywenderlich.com) to learn objective-c and yes always refer to [Apple](http://developer.apple.com) forums and documentations for everything. – madLokesh Feb 28 '14 at 06:41

3 Answers3

2

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.

Suryakant Sharma
  • 3,852
  • 1
  • 25
  • 47
  • Since when does @"" or @"a string" create anything that is autoreleased? That's nonsense. @"a string" is a pointer to an NSString object that lives as long as the application and will never be released. – gnasher729 Oct 29 '14 at 16:24
  • @gnasher729 you are right, `@""` doesn't create anything 'autorelease'. They are called `NSString` constant and exists throughout the program. thanks for pointing out this, I edited my answer. – Suryakant Sharma Oct 30 '14 at 08:22
1

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 );
Community
  • 1
  • 1
larva
  • 4,687
  • 1
  • 24
  • 44
0

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.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306