如何在Swift中使用USBmakebmRequestType

4

我使用以下代码在objective-c中设置USB请求类型:

#define USBmakebmRequestType

例如:

IOUSBDevRequest request;
request.bmRequestType = USBmakebmRequestType(kUSBOut, kUSBVendor, kUSBDevice);

我该如何在Swift中使用这个?Swift中没有USBmakebmRequestType函数。


这里到底是什么问题?你尝试过翻译代码,但卡在某个地方了吗? - Cristik
@Cristik,Swift中没有USBmakebmRequestType函数。 - Arti
1
USBmakebmRequestType 是一个 C 宏,无法导入到 Swift 中。你需要找到宏的原始定义,并自己将其转换为 Swift 函数。如果你能成功找到原始定义并将其添加到你的问题中,就会有人为你或你的赏金提供帮助。 - OOPer
@OOPer 哦,谢谢,我想我在 USB.h 的源代码中找到了它。我可以自己转换它 :) - Arti
那么你最好自己编写一个答案并接受它。搜索“USBmakebmRequestType”的开发人员将找到这篇文章,正确的答案会帮助他们。 - OOPer
1个回答

2
我自己实现了它:
func USBmakebmRequestType(direction:Int, type:Int, recipient:Int) -> UInt8 {
    return UInt8((direction & kUSBRqDirnMask) << kUSBRqDirnShift)|UInt8((type & kUSBRqTypeMask) << kUSBRqTypeShift)|UInt8(recipient & kUSBRqRecipientMask)
}

使用方法:

USBmakebmRequestType(direction: kUSBIn, type: kUSBDevice, recipient: kUSBStandard)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接