Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift swift swift

Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift


Update for Swift 5.1

For pointer types Swift provides pointee property,

Documentation for v5.1 says it is used for accessing instance referenced by this pointer

You can easily set the pointee field

myClass.isSSNField.pointee = false

And this is same for all pointer types and conversions. If you want to check the value of an Objective C BOOL* You can easily

if myClass.isSSNField.pointee.boolValue


I've never seen anything like the situation you describe, and personally I'm tempted to say the situation doesn't exist; I have never seen a BOOL* property or ivar in Objective-C in my life (and I'm darned old, believe me). However, if you insist: I haven't tested this, but I think you could say something like this:

var ok = UnsafeMutablePointer<ObjCBool>.alloc(1)ok[0] = false // or truelet val = okmyClass.isSSNField = val

However, although I think that will compile, I'm rather unclear on what the implications of doing it would be. It could cause the universe to collapse to a black hole, so be careful.


BOOL* in Objective-C is a pointer of Bool.
Use UnsafeMutablePointer in Swift.

var isSSNField: ObjCBool = truemyClass.isSSNField = &isSSNField

Then fix it.

Just like the isDirectory parameter in function:

func fileExists(atPath path: String, isDirectory:UnsafeMutablePointer<ObjCBool>?) -> Bool

You can use the code:

var isDirectory:ObjCBool = truevar isFileExists = FileManager.default.fileExists(atPath: <#YourPath#>, isDirectory: &isDirectory)