Why does dispatch_queue_create give an EXC_BAD_ACCESS error in Swift?

笑着哭i 提交于 2019-12-01 20:29:11

The second argument of dispatch_queue_create() has the type dispatch_queue_attr_t, which is declared as

typealias dispatch_queue_attr_t = NSObject

You have to pass DISPATCH_QUEUE_SERIAL or nil for a serial queue (or DISPATCH_QUEUE_CONCURRENT for a concurrent queue):

var thisQueue = dispatch_queue_create("com.myApp.mHitsUpdateQueue", DISPATCH_QUEUE_SERIAL)

In C(++), 0 can be passed instead of a NULL pointer.

The Swift compiler, however, wraps the integer 0 into an NSNumber object so that it can be passed to the function expecting an NSObject parameter. That causes the runtime exception because NSNumber is not a valid attribute. So passing 0 or nil is significantly different in Swift.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!