<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" 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<T></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=""> </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=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> array2 = [</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=""> </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=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</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="">.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=""> </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=""> }</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=""> var temp = self.array as NSArray</font></b></div><div style="margin: 0px; line-height: normal;" class=""><b class=""><font face="Menlo" class=""> ~~~~~^</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=""> 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=""> ~~~~~^</font></b></div><div class=""><b class=""><font face="Menlo" class=""><br class=""></font></b></div></div></span></div><div>Are there restrictions on what can be converted to NSArray?</div><div><br class=""></div><div>Doug Hill</div><div><br class=""><blockquote type="cite" class=""><div class="">On Jan 25, 2017, at 9:24 AM, Doug Hill via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" 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 <<a href="mailto:owenzx@gmail.com" class="">owenzx@gmail.com</a>> wrote:</div><br class="Apple-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=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></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:<wbr class="">using:) exists on NSArray in Swift. And I was able to create your generic class just fine:<div class=""><br class=""></div><div class="">class Test<T> {<br class=""> var array: [T] = []<br class=""> <br class=""> init() {<br class=""> var temp = array as NSArray<br class=""> }<br class="">}</div><div class=""><br class=""></div></div></blockquote></div></div></div></blockquote></div></div></div></div></div></div></blockquote></div><br class=""></div></body></html>