Search code examples
iosobjective-cosc

How to send two float in one package OSC


i need to send in one package two float numbers. I use CocoaOSC project https://github.com/danieldickison/CocoaOSC

how i call function to send:

[delegate sendPacket:@"/ShotHappends" value:[NSString stringWithFormat:@"%.3f %.3f", myXRound, myYRound] type:2];

my function

- (void)sendPacket:(NSString*)address value:(NSString*)sendValue type:(int)type
{
    defaults = [NSUserDefaults standardUserDefaults];
    remoteHost = [defaults stringForKey:@"host"];
    remotePort = [defaults stringForKey:@"port"];

    NSLog(@"Value: %@", sendValue);

    OSCMutableMessage *message = [[OSCMutableMessage alloc] init];
    message.address = address;

    sendType = type;

    switch (sendType)
    {
        case 0: [message addString:sendValue]; break;
        case 1: [message addInt:[sendValue intValue]]; break;
        case 2: [message addFloat:[sendValue floatValue]]; break;
        case 3: [message addBlob:[sendValue dataUsingEncoding:NSUTF8StringEncoding]]; break;
        case 4: [message addTimeTag:[NSDate date]]; break;
        case 5: [message addBool:YES]; break;
        case 6: [message addBool:NO]; break;
        case 7: [message addImpulse]; break;
        case 8: [message addNull]; break;
    }

    [connection sendPacket:message toHost:remoteHost port:[remotePort intValue]];
}

so as you see i create a string and say in my function what is in these string, if i say that string @"0,22 0,45" is float my server will get only first number, so how can i send two floats to my server? Thank you.


Solution

  • I haven't tested this, or even read the API, but I would imagine you would have to create a version of your method that accepts arrays of type/values:

    - (void)sendPacket:(NSString*)address
                values:(NSArray*)values
                 types:(NSArray*)types
    {
        NSAssert([values count] == [types count], @"Values/types array are different sizes!");
    
        defaults = [NSUserDefaults standardUserDefaults];
        remoteHost = [defaults stringForKey:@"host"];
        remotePort = [defaults stringForKey:@"port"];
    
        OSCMutableMessage *message = [[OSCMutableMessage alloc] init];
        message.address = address;
    
        for (NSUInteger i = 0; i < [values count]; i++)
        {
            int sendType = [[types objectAtIndex:i] intValue];
            id sendValue = [values objectAtIndex:i];
    
            switch (sendType)
            {
                case 0: [message addString:sendValue]; break;
                case 1: [message addInt:[sendValue intValue]]; break;
                case 2: [message addFloat:[sendValue floatValue]]; break;
                case 3: [message addBlob:[sendValue dataUsingEncoding:NSUTF8StringEncoding]]; break;
                case 4: [message addTimeTag:[NSDate date]]; break;
                case 5: [message addBool:YES]; break;
                case 6: [message addBool:NO]; break;
                case 7: [message addImpulse]; break;
                case 8: [message addNull]; break;
            }
        }
    
        [connection sendPacket:message toHost:remoteHost port:[remotePort intValue]];
    }
    

    Note: as you are passing the types (int) in an Objective-C collection class, they must be wrapped in NSNumber objects:

    [delegate sendPacket:@"/ShotHappends"
                  values:@[[NSString stringWithFormat:@"%.3f", myXRound],
                           [NSString stringWithFormat:@"%.3f", myYRound]
                          ]
                   types:@[ @(2), @(2) ]
    ];
    

    Note 2: An improvement to your method would be to pass strings as NSString, numbers/bools as NSNumber and data as NSData rather than using NSString all the time. Up to you, though.