ExistentialAudio/SwiftOSC

Parsing a OSCMessage

MacTuitui opened this issue · 2 comments

Hello there,

I'm having trouble to get to the data of any OSCMessage I'm getting. The example does not appear to compile with Swift 4.2.1

class OSCHandler: OSCServerDelegate {
    
    func didReceive(_ message: OSCMessage){
        if let integer = message.arguments[0] as Int {
            print("Received int \(integer)"
        } else {
            print(message)
        }
    }
}
server.delegate =  OSCHandler()

I'm getting the following: "'OSCType?' is not convertible to 'Int'; did you mean to use 'as!' to force downcast?" (and the typo with the missing ")" in the print statement).

I have to say I'm a complete beginner with Swift and this might be obvious for experts, but would you mind providing a working example where you go from didReceive to extracting for example a Int and a String? (And maybe do type checking?)

Thanks!

This compiles and works with 4.2.1:

if let iid = message.arguments[0] as? Int{
  //do stuff
}

Seems obvious after reading more about optionals, apologies.