Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift

YichenBman picture YichenBman · May 19, 2015 · Viewed 15.5k times · Source

I'm unfamiliar with Objective C.

I'm using a private framework and need to be able to change one of the properties from within my Swift code.

The property is declared in Objective C this way:

@property (nonatomic, assign) BOOL *isSSNField;

in swift I am trying to change the value of the property this way:

myClass.isSSNField = true

I am getting this error

Cannot assign a value of type 'Bool' to a value of type 'UnsafeMutablePointer<ObjcBool>'

I'm not sure where to go from here, or why I'm getting this error at all

Answer

accfews picture accfews · Dec 7, 2015

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