<div dir="ltr"><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" style=""><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">class Graph {</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">  func call(method: String) {</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">    // Something goes here</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">  }</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">  func redraw() -&gt; String {</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">    return &quot;Redraws&quot;</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">  }</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">}</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif"><br></font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">let inst = Graph()</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">inst.call(&quot;redraw&quot;)</font></div><div class="gmail_default" style=""><font face="arial, helvetica, sans-serif">---</font></div></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 10:18 AM, 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">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>    /// Construct a graphable item from a description.</div><div>    init(description: GraphableDescription)</div><div><br></div><div>    func graph()</div><div>}</div><div><br></div><div>// Example framework method.</div><div>func graphItem(description: GraphableDescription, graphable: Graphable.Type) {</div><div>    // Instantiate the graphable.</div><div>    let item = graphable.init(description: description)</div><div><br></div><div>    // Graph it.</div><div>    item.graph()</div><div>}</div><div><br></div><div>// Example Graphable client.</div><div>struct Circle: Graphable {</div><div>    init(description: GraphableDescription) { }</div><div><br></div><div>    func graph() { }</div><div>}</div><div><br></div><div>// Example framework client.</div><div>func foo() {</div><div>    graphItem(GraphableDescription(), graphable: Circle.self)</div><div>}</div></div><div>--</div><div><br></div><div><div> - Daniel</div><div><div class="h5"><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) <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.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 9:30 AM, Jan Neumüller <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</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">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:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!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:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!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:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
_______________________________________________<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>