× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Questions about function notation in Swift

Antonio Bello
Jul 14, 2015
<p>When defining parameters for a function or method, besides the type, each parameter must have a <strong>local name</strong>, which identifies the name of the variable accessible from the body of the function, but it can also have an <strong>external name</strong>, which is used when invoking the function.</p> <pre><code>func doSomething(externalName internalName: Int) { print(internalName) } doSomething(externalName: 4) </code></pre> <p>The underscore in place of the external name means that no external name is defined for that parameter. In the above example":</p> <pre><code>func doSomething(_ internalName: Int) { print(internalName) } </code></pre> <p>it means the function is invoked by passing the parameter without being prefixed by the external name:</p> <pre><code>doSomething(5) </code></pre> <p>External names are useful to identify what each parameter is for, making code easier to read. For instance, compare this:</p> <pre><code>update(33, 1, data) </code></pre> <p>with this:</p> <pre><code>update(invoiceId: 33, itemId: 1, invoiceDetails: data) </code></pre> <p>Because of its usefulness, Swift automatically "promotes" local names to external names <em>when not explicitly specified</em>. In Swift 1.x:</p> <ul> <li>all global function have no default external names</li> <li>all methods have all parameters but the first with an automatic generated external name</li> <li>all initializers have automatic generated external names</li> </ul> <p>In Swift 2.0 global functions follow the same rule as methods.</p> <p>Auto generating external names means for example that a function like this:</p> <pre><code>func updateInvoice(invoiceId: Int, invoiceDetailId: Int, invoiceDetail: data) </code></pre> <p>will have its signature automatically updated to:</p> <pre><code>func updateInvoice(invoiceId: Int, invoiceDetailId invoiceDetailId: Int, invoiceDetail invoiceDetail: data) ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^ </code></pre> <p>with the automatic addition of an external name to both the 2nd and the 3rd (i.e. all parameters after the 1st).</p> <p>In your case:</p> <pre><code>optional func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) </code></pre> <p>the first underscore means: <em>do not use nor generate an external name for this parameter</em>. As for the 2nd parameter, it is referenced as <code>didFinishPickingMediaWithInfo</code> when invoking the function, but it will be available to the <code>imagePickerController</code> methood as a variable named <code>info</code>.</p> <p>Recommended reading: <a href="https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID166" rel="nofollow">Function Parameter Names</a></p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/31366057/Questions%20about%20function%20notation%20in%20Swift/31366296">Stack Overflow</a>.</p>
comments powered by Disqus