Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Objective-C <strong>only</strong> support passing parameters by value. The problem here has probably been fixed already (Since this question is more than a year old) but I need to clarify some things regarding arguments and Objective-C.</p> <p><a href="http://en.wikipedia.org/wiki/Objective-C#Syntax" rel="noreferrer">Objective-C</a> is a <em>strict superset</em> of C which means that everything C does, Obj-C does it too.</p> <p>By having a quick look at Wikipedia, you can <a href="http://en.wikipedia.org/wiki/C_%28programming_language%29#Characteristics" rel="noreferrer">see</a> that <code>Function parameters are always passed by value</code></p> <p>Objective-C is no different. What's happening here is that whenever we are passing an object to a function (In this case a UILabel *), we pass the value <em>contained</em> at the pointer's address.</p> <p>Whatever you do, it will always be the value of what you are passing. If you want to pass the value of the <em>reference</em> you would have to pass it a **object (Like often seen when passing NSError).</p> <p>This is the same thing with scalars, they are passed by value, hence you can modify the value of the variable you received in your method and that won't change the value of the original variable that you passed to the function.</p> <p>Here's an example to ease the understanding:</p> <pre><code>- (void)parentFunction { int i = 0; [self modifyValueOfPassedArgument:i]; //i == 0 still! } - (void)modifyValueOfPassedArgument:(NSInteger)j { //j == 0! but j is a copied variable. It is _NOT_ i j = 23; //j now == 23, but this hasn't changed the value of i. } </code></pre> <p>If you wanted to be able to modify i, you would have to pass the value of the <em>reference</em> by doing the following:</p> <pre><code>- (void)parentFunction { int i = 0; //Stack allocated. Kept it that way for sake of simplicity [self modifyValueOfPassedReference:&amp;i]; //i == 23! } - (void)modifyValueOfPassedReference:(NSInteger *)j { //j == 0, and this points to i! We can modify i from here. *j = 23; //j now == 23, and i also == 23! } </code></pre>
    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. 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