I have searched but cannot find an example of this.
In my example I use playground to nest for-loops in a function called from within DispatchQueue.
I find many roadblocks:
//import AppKit // playground doesn't recognize AppKit?!
import Dispatch
import Foundation
var comboCount:NSKeyValueObservation?
//class viewContr: NSViewController {
@IBOutlet weak var progressBar: NSProgressIndicator!
// deinit { comboCount?.invalidate() }
@IBAction func calcStuff() {
DispatchQueue.global(qos: .userInitiated).async {
let combos = possibleCombos()
DispatchQueue.main.async {
for word in combos {
word.print0b8()
}
}
}
}
//}
func possibleCombos() -> [Int8] {
var combos = [Int8]()
for a in [0, 1] {
for b in [0, 1] {
for c in [0, 1] {
for d in [0, 1] {
combos.append(Int8(d | (c << 1) | (b << 2) | (a << 3)) )
comboCount = combos.count/16 as! NSKeyValueObservation
// known total combos = 16
// How do I pass combos.count out to a progress bar?
}
}
}
}
return combos
}
extension Int8 {func print0b8() {print("0b" + pad(string: String(self, radix: 2), toSize: 4))}}
func pad(string : String, toSize: Int) -> String {var padded = string;for _ in 0..<(toSize - string.count) {padded = "0" + padded};return padded}
Removing the @IB obstacles and the attempted declaration of a ViewController does produce output, but can't be used with a progress bar, e.g.,
import Dispatch
import Foundation
DispatchQueue.global(qos: .userInitiated).async {
let combos = possibleCombos()
DispatchQueue.main.async {
for word in combos {
word.print0b8()
}
}
}
func possibleCombos() -> [Int8] {
var combos = [Int8]()
for a in [0, 1] {
for b in [0, 1] {
for c in [0, 1] {
for d in [0, 1] {
combos.append(Int8(d | (c << 1) | (b << 2) | (a << 3)) )
// known total combos = 16
// How do I pass combos.count out to a progress bar?
}
}
}
}
return combos
}
extension Int8 {func print0b8() {print("0b" + pad(string: String(self, radix: 2), toSize: 4))}}
func pad(string : String, toSize: Int) -> String {var padded = string;for _ in 0..<(toSize - string.count) {padded = "0" + padded};return padded}
In my actual App, i.e., not this playground, I have a ProgressBar in my storyboard and an IBoutlet declared in my view controller. I just can't find how to pass a value out of "possibleCombos()" and into the NSProgressIndicator.
What I have read suggests a delegate but reading about delegates hasn't helped. I need an example.
Thanks.
It appears playground doesn't do macOS. When I go to Xcode/Preferences/Components the only entries are iOS, tvOS, and watchOS. So AppKit is not recognized.
When I do "New" > "Playground..." in Xcode 13, I have a choice between iOS and macOS. I'm not sure how to change it after the fact, but maybe you need to make a new Playground and choose macOS?
Yeah you are right mikander... and I forgot I did that. So yes I used a macOS playground. Then, there is no simulator for macOS. Hmm.