<div dir="ltr"><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. </div><div class="gmail_default" style="font-family:georgia,serif"><br></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></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 26, 2017 at 9:17 AM, Doug Hill <span dir="ltr">&lt;<a href="mailto:swiftusers@breaqz.com" target="_blank">swiftusers@breaqz.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"><div>I&#39;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><br></div><div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">class</span><span style="font-variant-ligatures:no-common-ligatures"> Test&lt;NSObject&gt;</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">{</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">let</span><span style="font-variant-ligatures:no-common-ligatures"> array</span><span style="font-variant-ligatures:no-common-ligatures"> = [</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">NSObject</span><span style="font-variant-ligatures:no-common-ligatures">]()</span></div><div style="margin:0px;line-height:normal;min-height:19px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">init</span><span style="font-variant-ligatures:no-common-ligatures">() {</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">let</span><span style="font-variant-ligatures:no-common-ligatures"> temp = </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">self</span><span style="font-variant-ligatures:no-common-ligatures">.array </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">as</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">NSArray</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div></div></div><div><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo"> error: cannot convert value of type &#39;[NSObject]&#39; to type &#39;NSArray&#39; in coercion</div><span class=""><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo">        var temp  = self.array  as NSArray</div></span><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo">                    ~~~~~^~~~~</div></div><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px;text-indent:0px"><br></span></div><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px;text-indent:0px">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"><span style="font-family:Helvetica;font-size:12px;text-indent:0px"><br></span></div><div style="margin:0px 0px 0px 12px;line-height:normal"><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">class</span><span style="font-variant-ligatures:no-common-ligatures"> Test&lt;T&gt;</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">{</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">let</span><span style="font-variant-ligatures:no-common-ligatures"> array = [</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">NSObject</span><span style="font-variant-ligatures:no-common-ligatures">]()</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal;min-height:19px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">init</span><span style="font-variant-ligatures:no-common-ligatures">() {</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">let</span><span style="font-variant-ligatures:no-common-ligatures"> temp = </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">self</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">array</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">as</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">NSArray</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span style="font-family:Helvetica;font-size:12px;text-indent:0px"><br></span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span style="font-family:Helvetica;font-size:12px;text-indent:0px"><br></span></div><div style="margin:0px;line-height:normal"><span style="text-indent:0px">I guess this is interesting, but I still can&#39;t create an array with items whose type is the type parameter of the class and then convert to NSArray.</span></div><span class="HOEnZb"><font color="#888888"><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span style="font-family:Helvetica;font-size:12px;text-indent:0px"><br></span></div><div style="font-family:Menlo;font-size:16px;margin:0px;line-height:normal"><span style="font-family:Helvetica;font-size:12px;text-indent:0px">Doug Hill</span></div></font></span></div><div><div class="h5"><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px;text-indent:0px"><br></span></div><div style="margin:0px 0px 0px 12px;font-size:11px;line-height:normal;font-family:Menlo"><br></div><div><blockquote type="cite"><div>On Jan 25, 2017, at 10:49 AM, Doug Hill &lt;<a href="mailto:swiftusers@breaqz.com" target="_blank">swiftusers@breaqz.com</a>&gt; wrote:</div><br class="m_8037418938997183771Apple-interchange-newline"><div><div style="word-wrap:break-word">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><br></div><div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">class</span><span style="font-variant-ligatures:no-common-ligatures"> Test&lt;T&gt;</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">{</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">var</span><span style="font-variant-ligatures:no-common-ligatures"> array:[</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">] = []</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">var</span><span style="font-variant-ligatures:no-common-ligatures"> array2 = [</span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">]()</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo;min-height:19px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">init</span><span style="font-variant-ligatures:no-common-ligatures">() {</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">var</span><span style="font-variant-ligatures:no-common-ligatures"> temp  = </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">self</span><span style="font-variant-ligatures:no-common-ligatures">.array </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">as</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">NSArray</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">var</span><span style="font-variant-ligatures:no-common-ligatures"> temp2 = </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">self</span><span style="font-variant-ligatures:no-common-ligatures">.array2 </span><span style="font-variant-ligatures:no-common-ligatures;color:#bb2ca2">as</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">NSArray</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;line-height:normal"><b><font face="Menlo">error: cannot convert value of type &#39;[T]&#39; to type &#39;NSArray&#39; in coercion</font></b></div><div style="margin:0px;line-height:normal"><b><font face="Menlo">                var temp = self.array as NSArray</font></b></div><div style="margin:0px;line-height:normal"><b><font face="Menlo">                           ~~~~~^</font></b></div><div style="font-family:Menlo;font-size:16px"><div style="font-family:Helvetica;font-size:12px;margin:0px;line-height:normal"><b><font face="Menlo">error: cannot convert value of type &#39;[T]&#39; to type &#39;NSArray&#39; in coercion</font></b></div><div style="font-family:Helvetica;font-size:12px;margin:0px;line-height:normal"><b><font face="Menlo">                var temp2 = self.array2 as NSArray</font></b></div><div style="font-family:Helvetica;font-size:12px;margin:0px;line-height:normal"><b><font face="Menlo">                            ~~~~~^</font></b></div><div><b><font face="Menlo"><br></font></b></div></div></span></div><div>Are there restrictions on what can be converted to NSArray?</div><div><br></div><div>Doug Hill</div><div><br><blockquote type="cite"><div>On Jan 25, 2017, at 9:24 AM, Doug Hill via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_8037418938997183771Apple-interchange-newline"><div><div style="word-wrap:break-word">Thanks for the help. I&#39;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&#39;m used to (even than C++!) so this answer helps figure out how the compiler works.<div><div><br></div><div>Doug Hill</div><div><br><div><br><div><blockquote type="cite"><div>On Jan 23, 2017, at 7:04 PM, Zhao Xin &lt;<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@gmail.com</a>&gt; wrote:</div><br class="m_8037418938997183771Apple-interchange-newline"><div><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">It seems to me that you didn&#39;t initialize your `<span style="font-size:12.800000190734863px;font-family:arial,sans-serif">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"><br></span></div><div class="gmail_default"><span style="font-size:12.800000190734863px">Zhaoxin</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 24, 2017 at 9:34 AM, Jon Shier via swift-users <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">enumerateObjects(options:using<wbr>:) exists on NSArray in Swift. And I was able to create your generic class just fine:<div><br></div><div>class Test&lt;T&gt; {<br>    var array: [T] = []<br>    <br>    init() {<br>        var temp = array as NSArray<br>    }<br>}</div><div><br></div></div></blockquote></div></div></div></blockquote></div></div></div></div></div></div></blockquote></div><br></div></div></div></blockquote></div><br></div></div></div></blockquote></div><br></div>