如何将UnsafePointer <UInt8>强制转换为UnsafePointer <UCKeyboardLayout>?

时间:2019-10-27 09:30:42

标签: swift pointers casting macos-carbon unsafe-pointers

Objective-C中有以下代码:

#import <Carbon/Carbon.h>

TISInputSourceRef inputSource = TISCopyCurrentKeyboardLayoutInputSource();
CFDataRef layoutData = (CFDataRef)TISGetInputSourceProperty(inputSource, kTISPropertyUnicodeKeyLayoutData);

const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);

我正在尝试将其转换为Swift,到目前为止,我已经拥有了:

import Carbon

let inputSource = TISCopyCurrentKeyboardLayoutInputSource()
let layoutData = TISGetInputSourceProperty(inputSource!.takeRetainedValue(), kTISPropertyUnicodeKeyLayoutData) as! CFData

let keyboardLayout = CFDataGetBytePtr(layoutData) as? UnsafePointer<UCKeyboardLayout>
// Cast from 'UnsafePointer<UInt8>?' to unrelated type 'UnsafePointer<UCKeyboardLayout>' always fails

我需要keyboardLayout成为UnsafePointer<UCKeyboardLayout>而不是UnsafePointer<UInt8>,以便可以将其传递到UCKeyTranslatekeyLayoutPtr参数中。

我该如何投放?

0 个答案:

没有答案