0

I'm trying to implement the Objective-C library, ORSSerialPort into my Swift project.

The example provided with the library provides the following setup for the ORSSerialPortManager class:

ORSSerialPortManager *portManager = [ORSSerialPortManager sharedSerialPortManager];

Should something such as this not be able to take it's place in Swift?

ORSSerialPortManager = ORSSerialPortManager.sharedSerialPortManager()

Perhaps with the pointer something like this?

ORSSerialPortManager = withUnsafePointer(&ORSSerialPortManager, ORSSerialPortManager.sharedSerialPortManager())

I get the errors: 'Cannot assign to the result of this expression', and 'Expressions are not allowed at the top level'. What has to change?

1
  • The first syntax is correct. Have you added the .h file for the Objective-C class to your bridging header? Commented Aug 21, 2014 at 12:27

1 Answer 1

1

Your expression:

ORSSerialPortManager = ORSSerialPortManager.sharedSerialPortManager()

is trying to assign to a type name (ORSSerialPortManager). This is the reason for the "Cannot assign to the result of this expression" error; the expression ORSSerialPortManager is not assignable. Instead, you want to assign to a new variable name:

let aPortManager = ORSSerialPortManager.sharedSerialPortManager()

Or, if you want a non-constant reference:

var aPortManager = ORSSerialPortManager.sharedSerialPortManager()

You can also put the type annotation on the variable, but it's not needed here (it can be deduced from the method signature):

var aPortManager : ORSSerialPortManager = ORSSerialPortManager.sharedSerialPortManager()

Notice the change in order for types on names: name : Type rather than Type name.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.