Reputation: 19116
In a class, I have the following init()
function:
init() {
let q = 0
dispatch_sync(queue) {
self._state = State(q)
}
}
where _state
is an instance of a struct State
and queue
a global dispatch queue.
I'm using the dispatch_sync
call in order to synchronize the potentially concurrently accessed instance of the class.
I'm running in some weird issue, that the compiler is complaining about using the _state
variable before it´s being initialized (namely using it in the block, before it is being initialized):
main.swift:363:37: error: variable 'self._state' used before being initialized
dispatch_sync(s_sync_queue) {
^
However, the sole purpose of using the dispatch queue and the block is to initialize the ivar.
The compiler even states, the code would return without initializing the variable _state
:
main.swift:372:5: error: property 'self._state' not initialized
}
^
albeit, clearly, due to dispatch_sync
the function init
cannot return without leaving the variable _state
uninitialized.
So, how could I solve the issue in an efficient manner?
Upvotes: 0
Views: 202
Reputation: 37189
One option is to declare _state as optional if you can.(If there is no harm to do that)
var _state:State?
if you make this optional you can use it in dispatch_sync
.
Upvotes: 1