Post

Replies

Boosts

Views

Activity

Async and Await code doesn't work on command line tools but works on Xcode playgrounds
I am running Mac OS 12.1 and Xcode 13.2. My code is the following: import Foundation import Cocoa func trapezoid(_ begin: Double, _ end: Double,_ n: Int) async -> Double { let hc = (end-begin)/Double(n) var h = begin var result = 0.0 for _ in 0...n{ result += h*h + (h+hc)*(h+hc) h = h+hc } return result * (hc/2) } Task{ let answer1 = await trapezoid(0, 1, 10000) let answer2 = await trapezoid(1, 2, 10000) print(answer1) print(answer2) } With playgrounds I get the following correct result: 0.33343334500044286 2.333733355000306 However, when I use the exact same code in an Xcode project using Command Line Tools, the program just returns with exit code 0 and no output. I am not sure what my error is or if it is an Xcode bug.
2
0
1.8k
Dec ’21