ExistentialAudio/SwiftOSC

How to send NSArray in OSCMessage

luiscript opened this issue · 3 comments

Hi!

I'm creating a SwiftOSC bridge for React Native, at some point my Javascript call resolves to a Swift function where I get a NSArray as parameter of type ANY.

My question is: how can I send that NSArray in an OSCMessage. Can someone provide an example of how to construct an [OSCType?] with different values to send in an OSCMessage?

func sendMessage(name: String, data: NSArray) -> Void {      
  let message = OSCMessage(OSCAddressPattern(name))
  //how to attach data (NSArray) to message (OSCMessage) ?

  //this does not work 
  for value in data {
     message.add( value )      //app crashes with this
  } 
  client.send(message)
}

Thank you for this framework

Thanks for your quick answer Devin, The array could have a mix of strings, floats, integers and booleans, for that reason the type of the array is NSArray.Element (ANY).

Hey! I just figured out how to do it. It was just a matter of casting down the ANY type of the array element to a basic type, I didn't know how to do that:

func sendMessage(name: String, data: NSArray) -> Void {
        let message = OSCMessage(OSCAddressPattern(name))
        
        for value in data {
            switch value {
                case let someInt as Int:
                    message.add(someInt)
                case let someDouble as Double where someDouble > 0:
                    message.add(someDouble)
                default:
                    print("something else")
            }
        }
        client.send(message)
    }

Sorry that this was a matter or Swift and not an issue of your framework. I'll let you know when my bridge for React Native is ready.

Thanks,

Luis Fernando