Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes; the Objective-C method syntax is like this for a couple of reasons; one of these is so that it is clear what the parameters you are specifying are. For example, if you are adding an object to an <code>NSMutableArray</code> at a certain index, you would do it using the method:</p> <pre><code>- (void)insertObject:(id)anObject atIndex:(NSUInteger)index; </code></pre> <p>This method is called <code>insertObject:atIndex:</code> and it is clear that an object is being inserted at a specified index.</p> <p>In practice, adding a string "Hello, World!" at index 5 of an <code>NSMutableArray</code> called <code>array</code> would be called as follows:</p> <pre><code>NSString *obj = @"Hello, World!"; int index = 5; [array insertObject:obj atIndex:index]; </code></pre> <p>This also reduces ambiguity between the order of the method parameters, ensuring that you pass the object parameter first, <em>then</em> the index parameter. This becomes more useful when using functions that take a large number of arguments, and reduces error in passing the arguments.</p> <p>Furthermore, the method naming convention is such because Objective-C doesn't support overloading; however, if you want to write a method that does the same job, but takes different data-types, this can be accomplished; take, for instance, the <code>NSNumber</code> class; this has several object creation methods, including:</p> <ul> <li><code>+ (id)numberWithBool:(BOOL)value;</code></li> <li><code>+ (id)numberWithFloat:(float)value;</code></li> <li><code>+ (id)numberWithDouble:(double)value;</code></li> </ul> <p>In a language such as C++, you would simply overload the number method to allow different data types to be passed as the argument; however, in Objective-C, this syntax allows several different variants of the same function to be implemented, by changing the name of the method for each variant of the function.</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