Parsing a OSCMessage
MacTuitui opened this issue · 2 comments
MacTuitui commented
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?)
devinroth commented
Yeah you have to cast it. Probably a slightly different syntax with Swift 4.2.1. I’m working on an updated version.
In the meantime you can use as! If you’re confident nothing else but an Int is but it’s saver to use as?.
You can see an example in the example project https://github.com/devinroth/SwiftOSC/blob/master/Examples/macOS/OSCMonitor/OSCMonitor/TableData.swift <https://github.com/devinroth/SwiftOSC/blob/master/Examples/macOS/OSCMonitor/OSCMonitor/TableData.swift>
… On Dec 19, 2018, at 9:13 PM, Alexis Andre ***@***.***> wrote:
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?)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub <#30>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AMaIEWPJU-krxiDWdC5_7JmL_9WhZM-dks5u6xxggaJpZM4ZbhjW>.
MacTuitui commented
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.