<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">You&#39;re right. I should be using a class instead…</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><br></div><div><div style="color:rgb(136,136,136);font-size:12.8000001907349px;max-width:600px;direction:ltr"><div style="max-width:470px;margin:8px 8px 0px 0px"><table border="0" cellspacing="0" cellpadding="0" width="470" style="width:470px"><tbody><tr valign="top"><td style="font-family:Arial;font-stretch:normal;font-size:14px;color:rgb(100,100,100);padding-left:10px"><div><b>Matthew Davies</b><br>Junior Developer, <a href="http://geostrategies.com" target="_blank">GeoStrategies</a></div><div>Director of Photography, <a href="http://offblockfilms.com" target="_blank">OffBlock Films</a></div><div style="color:rgb(141,141,141);font-size:13px;padding:5px 0px"><a href="tel:209-225.3246" style="color:rgb(141,141,141);text-decoration:none" target="_blank">209-225-3246</a> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="display:inline-block"><a href="tel:209-202-3284" style="color:rgb(141,141,141);text-decoration:none" target="_blank">209-202-3284</a></span> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="display:inline-block"><a href="mailto:daviesgeek@gmail.com" style="color:rgb(141,141,141);text-decoration:none" target="_blank">daviesgeek@gmail.com</a></span> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="white-space:nowrap;display:inline-block"><a href="https://daviesgeek.com/" style="color:rgb(141,141,141);text-decoration:none" target="_blank">daviesgeek.com</a></span></div><div style="margin-top:5px"><a href="http://facebook.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/facebook.png" width="16" height="16"></a> <a href="http://us.linkedin.com/in/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/linkedin.png" width="16" height="16"></a> <a href="http://twitter.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/twitter.png" width="16" height="16"></a> <a href="http://daviesgeek.com/feed.xml" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/blogRSS.png" width="16" height="16"></a>  <a href="http://github.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/github.png" width="16" height="16"></a></div></td></tr></tbody></table></div><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
<br><div class="gmail_quote">On Thu, Dec 10, 2015 at 3:47 PM, Daniel Dunbar <span dir="ltr">&lt;<a href="mailto:daniel_dunbar@apple.com" target="_blank">daniel_dunbar@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><span class=""><blockquote type="cite"><div>On Dec 10, 2015, at 1:01 PM, Matthew Davies via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Yes I have the protoco<div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline">​l​</div>, but the problem is that I would want to be able to call a method on the class that isn&#39;t necessarily defined in the protocol.<span> </span><br></div></div></blockquote><div><br></div></span>You should only call methods that are defined in the protocol, and you should extend the protocol as necessary. You can always have instances conform to additional protocols and use conditional casts, if necessary.</div><div><br></div><div>The value of this approach is that it is statically type safe, and users of your framework can easily tell what methods they need to implement to work with it.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div> - Daniel</div></font></span><div><div class="h5"><div><br><blockquote type="cite"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br>I.e., I would like to be able to do something like this<div class="gmail_default" style="font-family:arial,helvetica,sans-serif;display:inline">​:​</div><br><br><br><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">​---​</div><br><div>protocol Controller {</div><div>  init()</div><div>}</div><div><br></div><div>class MainController : Controller {</div><div><br></div><div>  required init() {}</div><div><br></div><div>  func index() -&gt; String {</div><div>    return &quot;This is the index&quot;</div><div>  }</div><div>}</div><div><br></div><div>class Router {</div><div>  func get(url: String, ctrl: Controller.Type, method: String) {</div><div>    let inst = ctrl.init()</div><div><b>    // Run the method that is passed in here</b></div><div>  }</div><div>}</div><div><br></div><div>let router = Router()</div><div>router.get(&quot;/&quot;, ctrl: MainController.self, method: &quot;index&quot;)</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">​---​</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Does that make sense as to what I&#39;m trying to accomplish? As I said, I&#39;m open to suggestions. I&#39;m relatively new to Swift&#39;s design patterns, so I may be thinking about this in completely the wrong way…</div><br><br><div class="gmail_quote"><div dir="ltr">On Thu, Dec 10, 2015 at 12:48 David Owens II &lt;<a href="mailto:david@owensd.io" target="_blank">david@owensd.io</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div>You have the protocol conformance, why can’t you simply call the method directly?</div></div><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On Dec 10, 2015, at 12:22 PM, Matthew Davies via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br><div><div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I&#39;m building a URL router in which I&#39;d like to pass a controller and a method. I don&#39;t want to instantiate all the controllers up front and pass the methods in as closures, nor do I want old controller instances still kept around. If there&#39;s a better way, I&#39;m definitely open to any suggestions. I&#39;m still learning the &quot;Swift&quot; way to do things.</div></div><div class="gmail_extra"><br clear="all"><div><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><br></div><div><div style="color:rgb(136,136,136);font-size:12.8px;max-width:600px;direction:ltr"><div style="max-width:470px;margin:8px 8px 0px 0px"><table border="0" cellspacing="0" cellpadding="0" width="470" style="width:470px"><tbody><tr valign="top"><td style="font-family:Arial;font-size:14px;color:rgb(100,100,100);padding-left:10px"><div><b>Matthew Davies</b><br>Junior Developer, <a href="http://geostrategies.com/" target="_blank">GeoStrategies</a></div><div>Director of Photography,<span> </span><a href="http://offblockfilms.com/" target="_blank">OffBlock Films</a></div><div style="color:rgb(141,141,141);font-size:13px;padding:5px 0px"><a href="tel:209-225.3246" style="color:rgb(141,141,141);text-decoration:none" target="_blank">209-225-3246</a> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="display:inline-block"><a href="tel:209-202-3284" style="color:rgb(141,141,141);text-decoration:none" target="_blank">209-202-3284</a></span> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="display:inline-block"><a href="mailto:daviesgeek@gmail.com" style="color:rgb(141,141,141);text-decoration:none" target="_blank">daviesgeek@gmail.com</a></span> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="white-space:nowrap;display:inline-block"><a href="https://daviesgeek.com/" style="color:rgb(141,141,141);text-decoration:none" target="_blank">daviesgeek.com</a></span></div><div style="margin-top:5px"><a href="http://facebook.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/facebook.png" width="16" height="16"></a> <a href="http://us.linkedin.com/in/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/linkedin.png" width="16" height="16"></a> <a href="http://twitter.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/twitter.png" width="16" height="16"></a> <a href="http://daviesgeek.com/feed.xml" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/blogRSS.png" width="16" height="16"></a>  <a href="http://github.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/github.png" width="16" height="16"></a></div></td></tr></tbody></table></div><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div><br><div class="gmail_quote">On Thu, Dec 10, 2015 at 10:47 AM, Dan Stenmark<span> </span><span dir="ltr">&lt;<a href="mailto:daniel.j.stenmark@gmail.com" target="_blank">daniel.j.stenmark@gmail.com</a>&gt;</span><span> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div>NSSelectorFromString() is still available in Swift, and you should be able to use the result of that in performSelector, though I’m hesitant to support this approach as it flies in the face of the safety Swift tries to enforce.  I’m curious about your use case here; are you trying to create some kind of dynamic proxy for a remote object ala NSXPCConnection?</div><span><font color="#888888"><div><br></div><div>Dan</div></font></span><div><div><br><div><blockquote type="cite"><div>On Dec 10, 2015, at 10:33 AM, Matthew Davies via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Ooh okay. I think that should work for my purposes. Thanks.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Somewhat related to this, how would I then call a method dynamically on an instance of the class, after instantiating it?</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">---</div><div class="gmail_default"><div class="gmail_default"><font face="arial, helvetica, sans-serif">class Graph {</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif"> <span> </span>func call(method: String) {</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif">   <span> </span>// Something goes here</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif"> <span> </span>}</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif"> <span> </span>func redraw() -&gt; String {</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif">   <span> </span>return &quot;Redraws&quot;</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif"> <span> </span>}</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif">}</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif">let inst = Graph()</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif">inst.call(&quot;redraw&quot;)</font></div><div class="gmail_default"><font face="arial, helvetica, sans-serif">---</font></div></div></div><div class="gmail_extra" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br clear="all"><div><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><br></div><div><div style="color:rgb(136,136,136);font-size:12.8px;max-width:600px;direction:ltr"><div style="max-width:470px;margin:8px 8px 0px 0px"><table border="0" cellspacing="0" cellpadding="0" width="470" style="width:470px"><tbody><tr valign="top"><td style="font-family:Arial;font-size:14px;color:rgb(100,100,100);padding-left:10px"><div><b>Matthew Davies</b><br>Junior Developer, <a href="http://geostrategies.com/" target="_blank">GeoStrategies</a></div><div>Director of Photography,<span> </span><a href="http://offblockfilms.com/" target="_blank">OffBlock Films</a></div><div style="color:rgb(141,141,141);font-size:13px;padding:5px 0px"><a href="tel:209-225.3246" style="color:rgb(141,141,141);text-decoration:none" target="_blank">209-225-3246</a> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="display:inline-block"><a href="tel:209-202-3284" style="color:rgb(141,141,141);text-decoration:none" target="_blank">209-202-3284</a></span> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="display:inline-block"><a href="mailto:daviesgeek@gmail.com" style="color:rgb(141,141,141);text-decoration:none" target="_blank">daviesgeek@gmail.com</a></span> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="white-space:nowrap;display:inline-block"><a href="https://daviesgeek.com/" style="color:rgb(141,141,141);text-decoration:none" target="_blank">daviesgeek.com</a></span></div><div style="margin-top:5px"><a href="http://facebook.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/facebook.png" width="16" height="16"></a> <a href="http://us.linkedin.com/in/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/linkedin.png" width="16" height="16"></a> <a href="http://twitter.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/twitter.png" width="16" height="16"></a> <a href="http://daviesgeek.com/feed.xml" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/blogRSS.png" width="16" height="16"></a>  <a href="http://github.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/github.png" width="16" height="16"></a></div></td></tr></tbody></table></div><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div><br><div class="gmail_quote">On Thu, Dec 10, 2015 at 10:18 AM, Daniel Dunbar<span> </span><span dir="ltr">&lt;<a href="mailto:daniel_dunbar@apple.com" target="_blank">daniel_dunbar@apple.com</a>&gt;</span><span> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">Note that you can define a protocol which will allow your framework to instantiate the type, and to call methods on instances of that type. If you can structure your code in this fashion, it can be very elegant in that it doesn&#39;t require factory functions and it is  type safe.<div><br></div><div>For example:</div><div>--</div><div><div>struct GraphableDescription { }</div><div><br></div><div>protocol Graphable {</div><div>   <span> </span>/// Construct a graphable item from a description.</div><div>   <span> </span>init(description: GraphableDescription)</div><div><br></div><div>   <span> </span>func graph()</div><div>}</div><div><br></div><div>// Example framework method.</div><div>func graphItem(description: GraphableDescription, graphable: Graphable.Type) {</div><div>   <span> </span>// Instantiate the graphable.</div><div>   <span> </span>let item = graphable.init(description: description)</div><div><br></div><div>   <span> </span>// Graph it.</div><div>   <span> </span>item.graph()</div><div>}</div><div><br></div><div>// Example Graphable client.</div><div>struct Circle: Graphable {</div><div>   <span> </span>init(description: GraphableDescription) { }</div><div><br></div><div>   <span> </span>func graph() { }</div><div>}</div><div><br></div><div>// Example framework client.</div><div>func foo() {</div><div>   <span> </span>graphItem(GraphableDescription(), graphable: Circle.self)</div><div>}</div></div><div>--</div><div><br></div><div><div> - Daniel</div><div><div><div><br><div><blockquote type="cite"><div>On Dec 10, 2015, at 9:59 AM, Matthew Davies via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br><div><div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><div class="gmail_default">I don&#39;t really like the idea of a factory function, but unfortunately that might be the only way to do it :( However, due to my specific use case, I don&#39;t think a factory function will work. I&#39;m working on a framework that will need to both instantiate the class from a string (or class type)<span> </span><i>and</i> call methods dynamically on it. Which, I&#39;m not sure I can do in the build tools that are provided in the open source package. Foundation hasn&#39;t been fully implemented and is missing a lot of the methods that would allow this to work.</div><div class="gmail_default"><br></div><div class="gmail_default">@Jens thanks for that blog post. I&#39;ll have to make sure I check back to see what his solution is for it.</div><div class="gmail_default"><br></div><div><br></div></div></div><div class="gmail_extra"><br clear="all"><div><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><br></div><div><div style="color:rgb(136,136,136);font-size:12.8px;max-width:600px;direction:ltr"><div style="max-width:470px;margin:8px 8px 0px 0px"><table border="0" cellspacing="0" cellpadding="0" width="470" style="width:470px"><tbody><tr valign="top"><td style="font-family:Arial;font-size:14px;color:rgb(100,100,100);padding-left:10px"><div><b>Matthew Davies</b><br>Junior Developer, <a href="http://geostrategies.com/" target="_blank">GeoStrategies</a></div><div>Director of Photography,<span> </span><a href="http://offblockfilms.com/" target="_blank">OffBlock Films</a></div><div style="color:rgb(141,141,141);font-size:13px;padding:5px 0px"><a href="tel:209-225.3246" style="color:rgb(141,141,141);text-decoration:none" target="_blank">209-225-3246</a> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="display:inline-block"><a href="tel:209-202-3284" style="color:rgb(141,141,141);text-decoration:none" target="_blank">209-202-3284</a></span> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="display:inline-block"><a href="mailto:daviesgeek@gmail.com" style="color:rgb(141,141,141);text-decoration:none" target="_blank">daviesgeek@gmail.com</a></span> <span style="color:rgb(102,102,102);display:inline-block">|</span> <span style="white-space:nowrap;display:inline-block"><a href="https://daviesgeek.com/" style="color:rgb(141,141,141);text-decoration:none" target="_blank">daviesgeek.com</a></span></div><div style="margin-top:5px"><a href="http://facebook.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/facebook.png" width="16" height="16"></a> <a href="http://us.linkedin.com/in/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/linkedin.png" width="16" height="16"></a> <a href="http://twitter.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/twitter.png" width="16" height="16"></a> <a href="http://daviesgeek.com/feed.xml" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/blogRSS.png" width="16" height="16"></a>  <a href="http://github.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank"><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/github.png" width="16" height="16"></a></div></td></tr></tbody></table></div><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div><br><div class="gmail_quote">On Thu, Dec 10, 2015 at 9:30 AM, Jan Neumüller<span> </span><span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span><span> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">Please no factory madness in Swift. This stuff is bad enough in Java - don’t infect Swift with it.<div><br></div><div>Jan<br><br><div><blockquote type="cite"><div><div><div>On 10.12.2015, at 18:23, Jens Alfke via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br></div></div><div><div style="word-wrap:break-word"><div><div><br><div><blockquote type="cite"><div>On Dec 10, 2015, at 7:26 AM, Harlan Haskins via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br><div><span style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">IIRC this isn’t possible because there’s no Runtime to query for classnames (it’s inherently unsafe anyway).</span></div></blockquote><div><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div></div><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">It’s not unsafe if you specify a base class/protocol that the loaded class must conform to.</div><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><blockquote type="cite"><div><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">You might want to look into a better way of doing that you’re trying to do.</div></div></blockquote></div><br><div>I disagree with “a better way” — “a workaround” is how I’d rephrase it. This kind of dynamism is often the best tool for the job, and a lot of Cocoa developers are frustrated by its absence in Swift. For example, there’s a series of blog posts from earlier this year by the highly respected Brent Simmons [NetNewsWire, MarsEdit, Glassboard, etc., currently at Omni]:</div><div><span style="white-space:pre-wrap">        </span><a href="http://inessential.com/swiftdiary" target="_blank">http://inessential.com/swiftdiary</a></div><div><span style="white-space:pre-wrap">        </span><a href="http://inessential.com/2015/07/20/swift_diary_1_class_or_struct_from_str" target="_blank">http://inessential.com/2015/07/20/swift_diary_1_class_or_struct_from_str</a></div><div><br></div><div>The workaround I’d suggest is a factory function that contains a switch statement that matches class names and returns newly initialized instances.</div><div><br></div><div>—Jens</div></div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=LDGmuA3DoapHmeUK-2Bv6cUW-2BPLkX9OuRniq9VPkW9gLj8xJjFiYUqXP-2B-2BqeLw1DrrZfro7eAuKJLpWeIY6RXsT6ndRrfTk-2FASv6KTPeHvqYeHVgEehXehfZsR17lSKZwhNl2XSAelASCAkxIKLhm1CUFBrefO15pHpakZ-2Fl3gz-2FRRwk0slXwlmlQMlzYQi36iHyltwjO1-2B7bmLCIF6zh3dAn0vnoY5QLWwaU1XSykJQ0-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"></div><span>_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></span></div></blockquote></div><br></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=eLFMrKDT8iBxZ-2Fbnk-2BZqvS8VFMZFE5zWEMX7mix5AIAkd3cIgkEMI23J6Zj58Nv4ENkYztTnnQxeopmIDZePfRVMD3G29FRSf7xu-2FEoK2qIUIm2ZQxKQEpVHUzh2TvtIev8TltG-2BH4zz4z7uQZcKhkYZORXIfwua1r6bjYj0l1tQt1XcGcMUeljsMHsy2WROtkT4NlS5SlmNPPsi1alCn7ZxCnUH9oKqX3JF7NFJ0J0-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"></div><br>_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br><br></blockquote></div><br></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=AdkfTiApI80cNEyortTzHbERtY5det-2FDBvSxuhs4q2PtyKQpyGCVmQsFQNbWeUdHppZ-2FflR60OwKBKNtU-2B7hU-2F5q7FNOB-2BrTdopT8flSnpdJ-2FHEDC9-2BXJgpzOTitnFOcAWcppZwjIwFd3Z3tMTGsl1Tv-2FiK6VBZ-2BMRoLnFk8KPshCSZZsk7mfU4JxB7V6xOFEUkLQBbBXRIoAvR2ZNusHAB2jONH9kh13gcdWT-2Bhtq0-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"><span> </span>_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></div></div></div></blockquote></div><br></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=AdkfTiApI80cNEyortTzHXP9hra5wYJZ9PjV6m-2BbvoFbb2NhTZhAkKMjOm9HY8TGNO0ntsXGVUCnD94FppyIKoUvdkdu9EfGisq3X4eum2fDS-2ByqwkHoXct3nM4pERPZpgidSJLdTQiY1EwPh8CC6Tg-2FlV2LAbP26LFfwBSvxc00bw9pPc1ChGf28BSBOKqq0hmYJ-2BaxFe-2BYYbAWyp1-2F55hoU8SbscnLpSLwP0-2B4GaU-3D" alt="" width="1" height="1" border="0" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important"><span> </span>_______________________________________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">swift-users mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="mailto:swift-users@swift.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-users@swift.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="https://lists.swift.org/mailman/listinfo/swift-users" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a></div></blockquote></div><br></div></div></div></blockquote></div><br></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=nE9rxSXA5G4kxsTVkgv43pXkLx-2B36P-2BPNJufHeY0dgctejNKvuXWEUIcGzwkJ8H2VnT5eXAs99o4J9ZOxacE-2BLLnp6iKyGaarObX8p-2B22ciFeCBKKYsPE-2B6wy3ofP2acxHVeUUcj4uatSgN47b6hFW-2F1GU1b3cq1-2BR-2BCzhweeeOPO25ekEIH3kLJ6Ve68NQPIltAX3zQq5RORSEqIeiZ7GGoB5XTxuQ2wT1ZxzUBRKw-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"><span> </span>_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></blockquote></div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=AdkfTiApI80cNEyortTzHbERtY5det-2FDBvSxuhs4q2OD6G6eSivPqkGsXWkbC5OkSMovn6ojBBiwqzG8oLWBEL4WiEAO3eusdMX3VL-2FUhPXRfoUng0G6gvpfWRH1gsKhlyQhbxJJEMg5-2Fq-2FQTdjGEj7imMrw5M24ygDKX32h0Z5L4FDOdnjYjjo7-2BofoHjweas-2Bi4-2BvV2UPVoCNnjmkfjmj7-2FW24t8klCgF32C43Nk0-3D" alt="" width="1" height="1" border="0" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important"><span> </span>_______________________________________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">swift-users mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="mailto:swift-users@swift.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-users@swift.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="https://lists.swift.org/mailman/listinfo/swift-users" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a></div></blockquote></div><br></div></div></div></blockquote></div><br></div>