简体   繁体   中英

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' Cannot assign value of type 'UnsafeMutableRawPointer!' to type 'UIView'

How to resolve this issue.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM