<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><blockquote type="cite" class="">On Nov 13, 2016, at 1:55 AM, Rick Mann <<a href="mailto:rmann@latencyzero.com" class="">rmann@latencyzero.com</a>> wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Nov 12, 2016, at 22:47 , David Sweeris <<a href="mailto:davesweeris@mac.com" class="">davesweeris@mac.com</a>> wrote:<br class=""><br class=""><br class=""><blockquote type="cite" class="">On Nov 13, 2016, at 00:38, Rick Mann via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class="">So, it seems there's still no way to do something like instantiate a class given only its name (as a String)?<br class=""><br class="">In my situation, I have a number of subclasses (or protocol implementations), which parallel some other class hierarchy. I need to dynamically create one based on the name of another. I don't want the latter classes to "know" anything about the former.<br class=""></blockquote><br class="">Not that I know of... If this is all your code, can you fake it by switching over the type's name?<br class=""></blockquote><br class="">Yeah, it just happens in a few places, would be nice to write the code once and then just ensure classes exist. I know I could do it subclassing NSObject, but Swift really ought to have proper reflection.<br class=""></blockquote><div class=""><br class=""></div>Reflection is on the todo list (although I can’t recall if it’s in-scope for Swift 4).<div class=""><br class=""></div><div class="">There’s also the possibility of putting the names of the classes you want to instantiate into an enum. In fact, if all the classes in question have a common superclass, you could this:</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span><span style="font-variant-ligatures: no-common-ligatures" class=""> MyAwesomeSuperClass { </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">required</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">() {} }</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span><span style="font-variant-ligatures: no-common-ligatures" class=""> MyAwesomeSubClass : </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyAwesomeSuperClass</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {}</span></div><div style="margin: 0px; line-height: normal; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> Whatever : </span><span style="font-variant-ligatures: no-common-ligatures" class="">MyAwesomeSuperClass</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span><span style="font-variant-ligatures: no-common-ligatures" class=""> Etc : </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Whatever</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {}</span></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">enum</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> ClassNames : </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">String</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">//Unfortunately the value of each case has to be a literal, so</span></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">//you can’t just say `case etc = "\(Etc.self)"`</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> myAwesomeSuperClass = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"MyAwesomeSuperClass"</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> myAwesomeSubClass = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"MyAwesomeSubClass"</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> whatever = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Whatever"</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> etc = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Etc"</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> instance: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyAwesomeSuperClass</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">ClassNames</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">init</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(rawValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">(</span><span style="font-variant-ligatures: no-common-ligatures;" class="">type(of: instance)</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">)"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)!</span> }</div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> type: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyAwesomeSuperClass</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type) {</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">ClassNames</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">init</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(rawValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">(</span><span style="font-variant-ligatures: no-common-ligatures;" class="">type</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">)"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)!</span> }</div><div style="margin: 0px; line-height: normal; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> type: </span><span style="font-variant-ligatures: no-common-ligatures" class="">MyAwesomeSuperClass</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.Type {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures" class="">myAwesomeSuperClass</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyAwesomeSuperClass</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">myAwesomeSubClass</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyAwesomeSubClass</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">whatever</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Whatever</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">etc</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Etc</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> etc = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">Etc</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> className = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">ClassNames</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">etc</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> newEtc = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">className</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">type</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">()</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; line-height: normal;" class=""><br class=""></div></span></div></div></div></blockquote><div class=""><div style="margin: 0px; line-height: normal; min-height: 14px;" class="">The same trick works if all the types in question conform to the same protocol, too:</div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures" class=""> MyAwesomeProtocol {</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> toIntMax() -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">IntMax</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> : </span><span style="font-variant-ligatures: no-common-ligatures" class="">MyAwesomeProtocol</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {}</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> Five : </span><span style="font-variant-ligatures: no-common-ligatures" class="">MyAwesomeProtocol</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> toIntMax() -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">IntMax</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(39, 42, 216);" class="">5</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> } </span>}</div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">enum</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ProtocolNames : </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">String</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> five = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Five"</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> int = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Int"</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> instance: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyAwesomeProtocol</span><span style="font-variant-ligatures: no-common-ligatures" class="">) { </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">ProtocolNames</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">init</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(rawValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">(</span><span style="font-variant-ligatures: no-common-ligatures;" class="">type(of: instance)</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">)”</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)! </span>}</div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> type: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyAwesomeProtocol</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type) {</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">ProtocolNames</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">init</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(rawValue: </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">(</span><span style="font-variant-ligatures: no-common-ligatures;" class="">type</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);" class="">)"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)!</span> }</div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> type: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyAwesomeProtocol</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Type {</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">switch</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">five</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Five</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span><span style="font-variant-ligatures: no-common-ligatures" class=""> .</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">int</span><span style="font-variant-ligatures: no-common-ligatures" class="">: </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> five = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Five()</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> fiveName = </span><span style="font-variant-ligatures: no-common-ligatures" class="">ProtocolNames</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">five</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div></div><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> newFive = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">fiveName</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">type</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div></div></blockquote><div style="margin: 0px; line-height: normal; min-height: 14px;" class=""><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div><div class="">Either way, though, you’ll have to do something like <span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">if</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">let</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> nf = </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);" class="">newFive</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">as</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">? </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Int</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> {…</span><span style="font-family: Menlo;" class="">}</span> if you want to use any functionality that isn’t explicitly in the superclass or protocol.</div><div class=""><br class=""></div><div class="">It certainly involves some boilerplate code, but it also has the advantages of validating the name before you try to use it <span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">and ensuring that any switch statements can actually handle all the classes. </span></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">- Dave Sweeris</span></span></div></body></html>