<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>You should try:</div><div id="AppleMailSignature">```</div><div id="AppleMailSignature">class Test&lt;T: NSObject&gt; {</div><div id="AppleMailSignature">&nbsp; &nbsp; let array: [T] = []</div><div id="AppleMailSignature">&nbsp; &nbsp; . . .</div><div id="AppleMailSignature">```</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">Because when you declare `class Test&lt;NSObject&gt;`, NSObject is the generic parameter, no longer the class.<br><br>Pierre</div><div><br>Le 26 janv. 2017 à 02:45, Doug Hill via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; a écrit&nbsp;:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><div class="">Unfortunately, this is for a work-related project, not just research, so I'm looking for solutions to specific problems that I'll need for my work.</div><div class=""><br class=""></div><div class="">I guess I'll have to file this one away as broken in Swift 2.x</div><div class=""><br class=""></div><div class="">Doug</div><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 25, 2017, at 5:28 PM, Zhao Xin &lt;<a href="mailto:owenzx@gmail.com" class="">owenzx@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:georgia,serif">I think in swift 2.x, the `Array` is not mature enough to do a lot of things. Besides, `NSArray` can also hold non-NSObjects like Int, for example.&nbsp;</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default" style="font-family:georgia,serif">I suggest you to use the latest Swift to do your research. As in Swift, everything moves fast and changes a lot. In the latest Xcode beta, all swift 2.x are abandoned. The latest stable swift is 3.0.2 and 2.3. The next release will be swift 3.1.</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Thu, Jan 26, 2017 at 9:17 AM, Doug Hill <span dir="ltr" class="">&lt;<a href="mailto:swiftusers@breaqz.com" target="_blank" class="">swiftusers@breaqz.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">I'm guessing that conversion of a Swift array to an NSArray can only happen if the Swift array holds NSObjects. So, I tried changing the type parameter of my class to NSObject:</div><div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo" 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=""> Test&lt;NSObject&gt;</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="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> array</span><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp;= [</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">NSObject</span><span style="font-variant-ligatures:no-common-ligatures" class="">]()</span></div><div style="margin:0px;line-height:normal;min-height:19px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </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" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp&nbsp;= </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">self</span><span style="font-variant-ligatures:no-common-ligatures" class="">.array&nbsp;</span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">as</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="">NSArray</span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; }</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=""><br class=""></span></div></div></div><div class=""><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp;error: cannot convert value of type '[NSObject]' to type 'NSArray' in coercion</div><span class=""><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; var temp&nbsp; = self.array&nbsp; as NSArray</div></span><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ~~~~~^~~~~</div></div><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-family:Helvetica;font-size:12px;text-indent:0px" class=""><br class=""></span></div><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-family:Helvetica;font-size:12px;text-indent:0px" class="">However, if I change the type parameter to something else it compiles with no problem.</span></div><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-family:Helvetica;font-size:12px;text-indent:0px" class=""><br class=""></span></div><div style="margin:0px 0px 0px 12px;line-height:normal" class=""><div style="font-family:Menlo;font-size:16px;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=""> Test&lt;T&gt;</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">{</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> array = [</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="">NSObject</span><span style="font-variant-ligatures:no-common-ligatures" class="">]()</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal;min-height:19px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </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="font-family:Menlo;font-size:16px;margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp = </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">self</span><span style="font-variant-ligatures:no-common-ligatures" class="">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">array</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">as</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="">NSArray</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; }</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal" class=""><span style="font-family:Helvetica;font-size:12px;text-indent:0px" class=""><br class=""></span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal" class=""><span style="font-family:Helvetica;font-size:12px;text-indent:0px" class=""><br class=""></span></div><div style="margin:0px;line-height:normal" class=""><span style="text-indent:0px" class="">I guess this is interesting, but I still can't create an array with&nbsp;items whose type is the type parameter of the class and then convert to NSArray.</span></div><span class="HOEnZb"><font color="#888888" class=""><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal" class=""><span style="font-family:Helvetica;font-size:12px;text-indent:0px" class=""><br class=""></span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal" class=""><span style="font-family:Helvetica;font-size:12px;text-indent:0px" class="">Doug Hill</span></div></font></span></div><div class=""><div class="h5"><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-family:Helvetica;font-size:12px;text-indent:0px" class=""><br class=""></span></div><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo" class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="">On Jan 25, 2017, at 10:49 AM, Doug Hill &lt;<a href="mailto:swiftusers@breaqz.com" target="_blank" class="">swiftusers@breaqz.com</a>&gt; wrote:</div><br class="m_8037418938997183771Apple-interchange-newline"><div class=""><div style="word-wrap:break-word" class="">OK, I just tried testing this code in my app and a Swift playground. I also tried a variation on the initializer just for the heck of it. I get the following error:<div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">class</span><span style="font-variant-ligatures:no-common-ligatures" class=""> Test&lt;T&gt;</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">{</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">var</span><span style="font-variant-ligatures:no-common-ligatures" class=""> array:[</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">T</span><span style="font-variant-ligatures:no-common-ligatures" class="">] = []</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">var</span><span style="font-variant-ligatures:no-common-ligatures" class=""> array2&nbsp;= [</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">T</span><span style="font-variant-ligatures:no-common-ligatures" class="">]()</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </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;font-size:16px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">var</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp&nbsp; = </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">self</span><span style="font-variant-ligatures:no-common-ligatures" class="">.array </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">as</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="">NSArray</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">var</span><span style="font-variant-ligatures:no-common-ligatures" class=""> temp2 = </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">self</span><span style="font-variant-ligatures:no-common-ligatures" class="">.array2 </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2" class="">as</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="">NSArray</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div style="margin:0px;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><div style="margin:0px;line-height:normal" class=""><b class=""><font face="Menlo" class="">error: cannot convert value of type '[T]' to type 'NSArray' in coercion</font></b></div><div style="margin:0px;line-height:normal" class=""><b class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var temp = self.array as NSArray</font></b></div><div style="margin:0px;line-height:normal" class=""><b class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;~~~~~^</font></b></div><div style="font-family:Menlo;font-size:16px" class=""><div style="font-family:Helvetica;font-size:12px;margin:0px;line-height:normal" class=""><b class=""><font face="Menlo" class="">error: cannot convert value of type '[T]' to type 'NSArray' in coercion</font></b></div><div style="font-family:Helvetica;font-size:12px;margin:0px;line-height:normal" class=""><b class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var temp2 = self.array2 as NSArray</font></b></div><div style="font-family:Helvetica;font-size:12px;margin:0px;line-height:normal" class=""><b class=""><font face="Menlo" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ~~~~~^</font></b></div><div class=""><b class=""><font face="Menlo" class=""><br class=""></font></b></div></div></span></div><div class="">Are there restrictions on what can be converted to NSArray?</div><div class=""><br class=""></div><div class="">Doug Hill</div><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jan 25, 2017, at 9:24 AM, Doug Hill via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="m_8037418938997183771Apple-interchange-newline"><div class=""><div style="word-wrap:break-word" class="">Thanks for the help. I'm still trying to figure out how Swift works, particularly what the error messages mean. This has been driving me a little nuts trying to figure out what is wrong via sometimes cryptic errors. Also, it seems like getting generic programming working in Swift is more difficult than I'm used to (even than C++!) so this answer helps figure out how the compiler works.<div class=""><div class=""><br class=""></div><div class="">Doug Hill</div><div class=""><br class=""><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 23, 2017, at 7:04 PM, Zhao Xin &lt;<a href="mailto:owenzx@gmail.com" target="_blank" class="">owenzx@gmail.com</a>&gt; wrote:</div><br class="m_8037418938997183771Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:georgia,serif">It seems to me that you didn't initialize your `<span style="font-size:12.800000190734863px;font-family:arial,sans-serif" class="">myArray` before you casted it. That caused the problem.</span></div><div class="gmail_default" style="font-family:georgia,serif"><span style="font-size:12.800000190734863px;font-family:arial,sans-serif" class=""><br class=""></span></div><div class="gmail_default"><span style="font-size:12.800000190734863px" class="">Zhaoxin</span></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Jan 24, 2017 at 9:34 AM, Jon Shier via swift-users <span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">enumerateObjects(options:using<wbr class="">:) exists on NSArray in Swift. And I was able to create your generic class just fine:<div class=""><br class=""></div><div class="">class&nbsp;Test&lt;T&gt; {<br class="">&nbsp; &nbsp;&nbsp;var&nbsp;array: [T] = []<br class="">&nbsp; &nbsp;&nbsp;<br class="">&nbsp; &nbsp;&nbsp;init() {<br class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;var&nbsp;temp =&nbsp;array&nbsp;as&nbsp;NSArray<br class="">&nbsp; &nbsp;&nbsp;}<br class="">}</div></div></blockquote></div></div></div></blockquote></div></div></div></div></div></div></blockquote></div></div></div></div></blockquote></div></div></div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-users mailing list</span><br><span><a href="mailto:swift-users@swift.org">swift-users@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br></div></blockquote></body></html>