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

How to use Swift generics to pass a type and return an object as that type?

Antonio Bello
Apr 22, 2015
<p>If you specify the type of the variable the return value is assigned to, type inference can do the rest. So if you have a <code>User</code> type, simply invoke the function as follows:</p> <pre><code>if let user: User = MQFileManager.objectWithFileName("User", inFolder: .DocumentDirectory) { } </code></pre> <p>Since the <code>user</code> variable is of <code>User</code> type, the compiler can infer the <code>T</code> generic to be of <code>User</code> type</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/29674548/How%20to%20use%20Swift%20generics%20to%20pass%20a%20type%20and%20return%20an%20object%20as%20that%20type?/29675804">Stack Overflow</a>.</p>
comments powered by Disqus