<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="">For reasons, I'm still using Xcode 7 and Swift 2.2<div class=""><br class=""></div><div class=""> Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31)</div><div class=""><br class=""></div><div class="">Doug Hill</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 25, 2017, at 5:13 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"><span style="font-family:georgia,serif" class="">Can you check the version of you Swift? Your code works in Xcode 8.2.1 (8C1002),</span><font face="georgia, serif" class=""> Swift <span style="" class="">3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)</span></font></div><div class="gmail_default"><span style="" class=""><font face="georgia, serif" class=""><br class=""></font></span></div><div class="gmail_default"><span style="" class=""><font face="georgia, serif" class="">Zhaoxin</font></span></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Thu, Jan 26, 2017 at 2:49 AM, Doug Hill <span dir="ltr" class=""><<a href="mailto:swiftusers@breaqz.com" target="_blank" class="">swiftusers@breaqz.com</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="">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=""><span 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></span><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 class="">Are there restrictions on what can be converted to NSArray?</div><span class="HOEnZb"><font color="#888888" class=""><div class=""><br class=""></div><div class="">Doug Hill</div></font></span><span class=""><div class=""><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" target="_blank" class="">swift-users@swift.org</a>> wrote:</div><br class="m_-3424256056341011894Apple-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 <<a href="mailto:owenzx@gmail.com" target="_blank" class="">owenzx@gmail.com</a>> wrote:</div><br class="m_-3424256056341011894Apple-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: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 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=""></span></div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></body></html>