-2

Yesterday when I was making a label for my project I discovered that I can use two formats for displaying text on screen:

label.text = @"Hello world";

And

NSString* textForLabel =[NSString stringWithFormat:@"Hello world"];
[label setText:textForLabel];

They both do the same thing.
Question - which one is better to use for app development and why?

ACerts
  • 308
  • 1
  • 6
  • 22
artG
  • 235
  • 3
  • 12
  • 1
    Read http://stackoverflow.com/questions/11642735/difference-between-text-and-settext and http://stackoverflow.com/questions/2207810/is-there-a-difference-between-setting-a-property-with-the-dot-or-the-bracket-syn . – Hima Mar 09 '16 at 07:43

2 Answers2

2

StringwithFormat is used in those cases when dynamically you want to pass some value in string.

Morever refer :-

http://programmingbulls.com/stringwithformat

So if you just need to pass a string without need to pass any particular value in it then do not use stringwithformat.

Awesome.Apple
  • 1,316
  • 1
  • 11
  • 24
2

The first one, because it's shorter, more obvious and it doesn't create a string from a string where that string isn't a format but incurs the processing cost of checking it as a format just to find it has no parameter insertion.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • 1
    the first one - in this particular case only. The second if you're building a string with parameters. – Russell Mar 09 '16 at 07:56