<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Thanks for the tip about 'concurrentPerform'!<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 23, 2017, at 5:34 PM, Jon Shier <<a href="mailto:jon@jonshier.com" class="">jon@jonshier.com</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="">enumerateObjects(options: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 class="">I’m not sure what the canonical parallel array enumeration would be, but you can do it using concurrentPerform:</div><div class=""><br class=""></div><div class="">let array = [“one”, “two”]</div><div class="">DispatchQueue.concurrentPerform(iterations: array.count) { index in<br class=""> print(array[index])<br class="">}</div><div class=""><br class=""></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jan 23, 2017, at 8:20 PM, 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=""><div class="">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 class=""><br class=""></div><div class="">var myNSArray : NSArray = mySwiftArray as NSArray</div><div class=""><br class=""></div><div class="">Here's the problem I'm running into; I have the following class:</div><div class=""><br class=""></div><div class=""><div class="">class Tester<typeA></div><div class="">{</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var myArray : [typeA]</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>init()</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>var temp = self. myArray as NSArray</div><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}</div><div class="">}</div></div><div class=""><br class=""></div><div class="">Which produces a compiler error:</div><div class=""><br class=""></div><div class="">'<span style="font-family: Menlo; font-size: 11px; text-indent: -12px;" class="">cannot convert value of type '[typeA]' to type 'NSArray' in coercion'</span></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">Doug Hill</div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></body></html>