Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Always try to do what feels natural. If you're assigning a constant string then do that, i.e. the first option. <code>@"..."</code> strings are very efficient constants that do not need to be memory managed, so use them if it makes sense. </p> <pre><code>NSLog(@"%p", @"XX"); NSLog(@"%p", @"XX"); NSLog(@"%p", @"XX"); </code></pre> <p>Results in:</p> <pre><code>0xa2424 0xa2424 0xa2424 </code></pre> <p>i.e. They are all the same object in memory.</p> <pre><code>NSLog(@"%p", [NSString stringWithString:@"XX"]); NSLog(@"%p", @"XX"); NSLog(@"%p", [NSString stringWithString:@"XX"]); </code></pre> <p>Also results in:</p> <pre><code>0xa2424 0xa2424 0xa2424 </code></pre> <p>As you can see from this there is no difference between the two objects, thus using <code>-stringWithString:</code> is just an extra message to send. Having said that, the overhead is usually not big enough to make a difference, so it shouldn't be a big deal either way. Personally I'd go with method one as there is no benefit of using method two, it's just extra code.</p> <p>However,</p> <pre><code>NSLog(@"%p", [NSString stringWithFormat:@"XX"]); NSLog(@"%p", [NSString stringWithFormat:@"XX"]); NSLog(@"%p", [NSString stringWithFormat:@"XX"]); </code></pre> <p>Results in:</p> <pre><code>0x7f86730 0xf8479b0 0x8a4cdb0 </code></pre> <p>As you can see, a new string is created each time as the sting you provide is just a format string that is used to process the following substitution variables, as you have none avoid <code>stringWithFormat:</code> unless you need it.</p> <p>(Obviously all addresses are examples...)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload