Objective c 目标C到Swift、指针转换

Objective c 目标C到Swift、指针转换,objective-c,pointers,swift,Objective C,Pointers,Swift,我正在尝试将Objective-C库或SerialPort实现到我的Swift项目中 随库提供的示例为ORSSerialPortManager类提供了以下设置: ORSSerialPortManager *portManager = [ORSSerialPortManager sharedSerialPortManager]; 像这样的东西不能在Swift中取代它吗 ORSSerialPortManager = ORSSerialPortManager.sharedSerialPortMana

我正在尝试将Objective-C库或SerialPort实现到我的Swift项目中

随库提供的示例为ORSSerialPortManager类提供了以下设置:

ORSSerialPortManager *portManager = [ORSSerialPortManager sharedSerialPortManager];
像这样的东西不能在Swift中取代它吗

ORSSerialPortManager = ORSSerialPortManager.sharedSerialPortManager()
也许是用这样的指针

ORSSerialPortManager = withUnsafePointer(&ORSSerialPortManager, ORSSerialPortManager.sharedSerialPortManager())
我得到错误:“无法分配到此表达式的结果”和“不允许在顶层使用表达式”。有什么需要改变的?

你的表情:

ORSSerialPortManager = ORSSerialPortManager.sharedSerialPortManager()
正在尝试分配给类型名称(
或SerialPortManager
)。这就是“无法分配到此表达式的结果”错误的原因;表达式
或SerialPortManager
不可赋值。相反,您希望分配给一个新的变量名:

let aPortManager = ORSSerialPortManager.sharedSerialPortManager()
或者,如果需要非常量引用:

var aPortManager = ORSSerialPortManager.sharedSerialPortManager()
您也可以将类型注释放在变量上,但这里不需要它(可以从方法签名推断):


注意名称上的类型顺序的变化:
name:Type
而不是
Type name

第一个语法是正确的。您是否已将Objective-C类的.h文件添加到桥接头中?
var aPortManager : ORSSerialPortManager = ORSSerialPortManager.sharedSerialPortManager()