Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Pass by reference in Objective-C is almost never the right way.</h1> <p>There are a number of problems with that code.</p> <ul> <li><p><code>(id**)</code> is a <em>pointer to a pointer to a pointer to an object</em>. Probably not at all what you want.</p></li> <li><p><code>YES</code> and <code>NO</code> are <code>BOOL</code> return types; not <code>TRUE</code></p></li> <li><p>there is no reason in that code to be returning something by reference.</p></li> <li><p>method names start with lower case letters. Arguments do, too.</p></li> <li><p>There will never be an instance of <code>NSMutableArray</code> in an application; just subclasses</p></li> <li><p>You can't tell the difference between a mutable and immutable array in the first place; check for <code>isKindOfClass:</code> or <code>isMemberOfClass:</code> for an <code>NSMutableArray</code> won't do you <em>much</em> good (it is useful, but misleading).</p></li> </ul> <p>This is better:</p> <pre><code>-(BOOL)remoteCall: (id) dataThing { if([dataThing isKindOfClass:[NSMutableArray class]] == YES) { NSMutableArray *swArray = dataThing; // not strictly necessary, but good defensive practice //do something with swArray return YES; } return NO; } </code></pre> <p>To be called like:</p> <pre><code>NSMutableArray * channelArray = [[NSMutableArray alloc]init]; // you'll need to release this somewhere Services * serv = [[Services alloc] init]; return [serv remoteCall:channelArray]; </code></pre> <p>Since you don't return a different array in <code>remoteCall:</code>, <code>channelArray</code>'s contents will be manipulated by the method and the YES/NO return value.</p> <p>If there is some reason why the above seemingly won't work for you, please explain why.</p> <p><b>Note:</b> The code obviously requires an <code>NSMutableArray</code> if you are going to muck with the contents. The <code>isKindOfClass:</code> could be checking for <code>NSMutableArray</code> or <code>NSArray</code> and it wouldn't matter either way. When using arrays in your code and requiring a mutable array, it is up to you to make sure the data flow is correct such that you don't end up w/an immutable array where you need a mutable array.</p>
 

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