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

Swift "with" keyword

Antonio Bello
Mar 01, 2015
<p><code>with</code> is <strong>not</strong> a keyword - it's just an external parameter identifier. This works as well:</p> <pre><code>func toDebugString&lt;T&gt;(whatever x: T) -&gt; String </code></pre> <p>Since the <code>toDebugString&lt;T&gt;(x: T)</code> function is already defined, by using an external parameter you are creating an <em>overload</em>: same function name, but different parameters. In this case the parameter is the same, but identified with an external name, and in swift that makes it a method with a different signature, hence an overload.</p> <p>To prove that, paste this in a playground:</p> <pre><code>func toDebugString&lt;T&gt;(# x: T) -&gt; String { return "overload" } toDebugString(x: "t") // Prints "overload" toDebugString("t") // Prints "t" </code></pre> <p>The first calls your overloaded implementation, whereas the second uses the existing function</p> <p>Suggested reading: <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-XID_254">Function Parameter Names</a></p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/25583477/Swift%20%22with%22%20keyword/25583840">Stack Overflow</a>.</p>
comments powered by Disqus