<div dir="ltr"><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">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"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></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:<wbr>using:) exists on NSArray in Swift. And I was able to create your generic class just fine:<div><br></div><div>class Test<T> {<br> var array: [T] = []<br> <br> init() {<br> var temp = array as NSArray<br> }<br>}</div><div><br></div><div>I’m not sure what the canonical parallel array enumeration would be, but you can do it using concurrentPerform:</div><div><br></div><div>let array = [“one”, “two”]</div><div>DispatchQueue.<wbr>concurrentPerform(iterations: <wbr>array.count) { index in<br> print(array[index])<br>}</div><div><br></div><div><br><div><blockquote type="cite"><div><div class="h5"><div>On Jan 23, 2017, at 8:20 PM, Doug Hill via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_3717231692475968104Apple-interchange-newline"></div></div><div><div><div class="h5"><div style="word-wrap:break-word"><div>I'm trying to accomplish the equivalent functionality of -[NSArray enumerateUsingObjects:…] in Swift. Doing a Googles search, I see that one would need to call the equivalent method on the bridged NSArray version of your Swift array:</div><div><br></div><div>var myNSArray : NSArray = mySwiftArray as NSArray</div><div><br></div><div>Here's the problem I'm running into; I have the following class:</div><div><br></div><div><div>class Tester<typeA></div><div>{</div><div><span class="m_3717231692475968104Apple-tab-span" style="white-space:pre-wrap">        </span>var myArray : [typeA]</div><div><br></div><div><span class="m_3717231692475968104Apple-tab-span" style="white-space:pre-wrap">        </span>init()</div><div><span class="m_3717231692475968104Apple-tab-span" style="white-space:pre-wrap">        </span>{</div><div><span class="m_3717231692475968104Apple-tab-span" style="white-space:pre-wrap">                </span>var temp = self. myArray as NSArray</div><div><span class="m_3717231692475968104Apple-tab-span" style="white-space:pre-wrap">        </span>}</div><div>}</div></div><div><br></div><div>Which produces a compiler error:</div><div><br></div><div>'<span style="font-family:Menlo;font-size:11px">cannot convert value of type '[typeA]' to type 'NSArray' in coercion'</span></div><div><br></div><div>Ok, this makes some sense since I'm guessing NSArray requires each element to to be an NSObject but this array type Array<typeA> could be a non-NSObject.</div><div><br></div><div>However, this makes my code harder to write since I now have to make sure any array has element type NSObject to use enumerateUsingObjects. Not something I can either guarantee or even desire.</div><div><br></div><div>The reason I like enumerateUsingObjects is that it supports a functional style of programming and is better at creating work items for each object by dispatching each array item on multiple cores/processors/threads for me. Writing this method myself would require figuring out to pass an object to a dispatch invocation. But looking through the swift API's, I don't see any GCD method for passing an object to dispatch_sync/async. I see versions of these methods that takes a context parameter but then takes a C function instead of a block, so not very Swift-like and potentially unsafe.</div><div><br></div><div>Does this mean enumerateUsingObjects is generally not all that useful in Swift? Are there better alternatives? Any ideas on how best to handle this situation would be appreciated.</div><div><br></div><div>Doug Hill</div></div></div></div>______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>