Vineesh TP
Vineesh TP

Reputation: 7973

Cannot assign value of type 'UnsafeMutableRawPointer!' to type 'UIView'

I am trying to convert a Objective-C source code to Swift.

In Objective-C the below code

myView = (__bridge UIView *)([SmartPlayerSDK SmartPlayerCreatePlayView:0 y:0 width:screenWidth height:playerHeight]);

where myView is a UIView

I am using a library the function is given below,

+ (void*)SmartPlayerCreatePlayView:(NSInteger)x y:(NSInteger)y width:(NSInteger)width height:(NSInteger)height;

It return the address.

I am trying to convert to Swift

myView = SmartPlayerSDK.smartPlayerCreatePlayView(0, y: 0, width: 10, height: 10)

smartPlayerCreatePlayView - Automatically convert to small letter

got error: - Cannot assign value of type 'UnsafeMutableRawPointer!' to type 'UIView'

How to resolve this issue.

Upvotes: 0

Views: 1778

Answers (1)

Martin R
Martin R

Reputation: 540065

Bridging between raw pointers and managed object pointers is done in Swift using the Unmanaged type. In your case the equivalent code would be

let rawPointer = SmartPlayerSDK.smartPlayerCreatePlayView(0, y: 0, width: 10, height: 10)
let view = Unmanaged<UIView>.fromOpaque(rawPointer).takeUnretainedValue()

If smartPlayerCreatePlayView() returns a (+1) retained object reference (as the "Create" in the method name indicates) then it should be

let view = Unmanaged<UIView>.fromOpaque(rawPointer).takeRetainedValue()

to balance that, otherwise you have a memory leak.

For more bridging conversions, helper functions, and links to the documentation, see How to cast self to UnsafeMutablePointer<Void> type in swift.

Upvotes: 2

Related Questions