Error initialising UnsafeMutablePointer<ObjCBool>

The following code compiles ok in one function:

Code Block        
let stop:UnsafeMutablePointer<ObjCBool>
stop.initialize(to: false)
text.enumerateAttribute(kStyleKey,
                                in: NSRange(location: 0, length: text.string.count),
                                options: []) { (value, range, stop) in

but the following code which is virtually the same fails the initialise with the error "Constant 'stop' being used before being initialised" :

Code Block        
let stop:UnsafeMutablePointer<ObjCBool>
stop.initialize(to: false)
attributedText.enumerateAttribute(kStyleKey,
                                          in: NSRange(location: selectedRange.location, length: selectedRange.length),
                                options: []) { (value, range, stop) in

Any suggestions - looks like a compiler issue to me.

Answered by Claude31 in 665187022
Thanks for feedback. Don't forget to close the thread.
You may be mistaking something. Your first code causes error Constant 'stop' used before being initialized, even put in one function.

What are text, kStyleKey, attributedText or selectedRange?

Please show more context. And please re-check if your code is enough to reproduce the issue.
Thanks for the reply. I was mistaking something - there is no need to declare 'stop' outside the closure! That said it doesn't explain the error. The following produces the same error, which I reckon is a compiler bug:

Code Block
import Foundation
class Test {
    func test() {
        let stop:UnsafeMutablePointer<ObjCBool>
        stop.initialize(to: false)
    }
}


The following produces the same error, which I reckon is a compiler bug

I do not understand why you think your new code would be a compiler bug.
You declare a let constant stop without initial value,
so you need to assign an initial value to it before it is used.

But you are using it and that is causing the compiler error you described.

Why do you think your new code is valid?
If you look at the documentation for UnsafeMutablePointer you will see that the memory pointed to has to be initialised, not the pointer itself and that the initialize method does this. This is confirmed by declaring stop as a var as in:

Code Block
    func test() {
        var stop:UnsafeMutablePointer<ObjCBool>
        stop.initialize(to: false)
    }

This still fails the same way. I will report it as a compiler bug.
I think that enumerateAttribute initialises it to false anyway, which is why this code isn't needed in the first place.

If you look at the documentation for UnsafeMutablePointer you will see that the memory pointed to has to be initialised, not the pointer itself and that the initialize method does this.

You are misinterpreting the documentation. The doc says you need to initialize the region allocated, but it does not say anything about the variable initialization. You may be confused with calling initialize and giving an initial value to a variable declared.

This still fails the same way. I will report it as a compiler bug.

NO. It is your misinterpreting the documentation.
Ok, so how do you think the code should be written?

Ok, so how do you think the code should be written?

That depends on where you want the pointer point to.


Seems you are not accustomed to managing pointers in Swift.

For example, if you want a pointer pointing a newly allocated region in heap:
Code Block
func test() {
let stop: UnsafeMutablePointer<ObjCBool> = UnsafeMutablePointer.allocate(capacity: 1)
stop.initialize(to: false)
//... Use `stop` ...
stop.deinitialize(count: 1)
stop.deallocate()
}


If you want to manage heap allocation by yourself, please do not forget these allocate-initialize-deinitialize-deallocate life cycle of pointer.
(Not only initialize. You can abbreviate it as AIDD.)

Or you want a pointer to some other thing?
Read this:
https://developer.apple.com/documentation/swift/unsafemutablepointer

Code Block
        let stop = UnsafeMutablePointer<ObjCBool>.allocate(capacity: 1)

Thank you for this. No, I’m not used to using pointers in Swift. I try and steer clear of them. As I mentioned earlier these lines aren’t necessary anyway because emumerateAttribute deals with setting it up. Definitely a case of RTM (polite version).
Accepted Answer
Thanks for feedback. Don't forget to close the thread.
Error initialising UnsafeMutablePointer&lt;ObjCBool&gt;
 
 
Q