在命令行工具 Xcode 的控制台中没有打印出任何内容
Nothing prints out in the console in command line tool Xcode
当我 运行 以下代码时,命令行工具 Xcode 的控制台中没有打印任何内容:
import Foundation
class A {
var someValue = 0
let concurrentQueue = dispatch_queue_create("queue_for_property", DISPATCH_QUEUE_CONCURRENT)
func increaseValueBy1000() {
dispatch_barrier_async(concurrentQueue) {
for _ in 0 ..< 1000 {
let v = self.someValue + 1
print(v)
self.someValue = v
}
}
}
}
let instance1 = A()
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)) {
instance1.increaseValueBy1000()
}
instance1.increaseValueBy1000()
我在控制台中没有看到任何打印语句。
如果我删除障碍线效果很好。
在这种情况下我做错了什么为什么我的障碍不允许打印?
应用程序——例如有命令行程序——还没有 "run loop" 必须调用
dispatch_main() // Swift 2
dispatchMain() // Swift 3
为了使用 GCD。来自 documentation:
This function "parks" the main thread and waits for blocks to be
submitted to the main queue. Applications that call UIApplicationMain
(iOS), NSApplicationMain
(Mac OS X), or CFRunLoopRun
on the main
thread must not call dispatch_main
.
当我 运行 以下代码时,命令行工具 Xcode 的控制台中没有打印任何内容:
import Foundation
class A {
var someValue = 0
let concurrentQueue = dispatch_queue_create("queue_for_property", DISPATCH_QUEUE_CONCURRENT)
func increaseValueBy1000() {
dispatch_barrier_async(concurrentQueue) {
for _ in 0 ..< 1000 {
let v = self.someValue + 1
print(v)
self.someValue = v
}
}
}
}
let instance1 = A()
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)) {
instance1.increaseValueBy1000()
}
instance1.increaseValueBy1000()
我在控制台中没有看到任何打印语句。 如果我删除障碍线效果很好。 在这种情况下我做错了什么为什么我的障碍不允许打印?
应用程序——例如有命令行程序——还没有 "run loop" 必须调用
dispatch_main() // Swift 2
dispatchMain() // Swift 3
为了使用 GCD。来自 documentation:
This function "parks" the main thread and waits for blocks to be submitted to the main queue. Applications that call
UIApplicationMain
(iOS),NSApplicationMain
(Mac OS X), orCFRunLoopRun
on the main thread must not calldispatch_main
.