1

我需要在一个包裹中发送两个浮点数。我使用 CocoaOSC 项目https://github.com/danieldickison/CocoaOSC

我如何调用函数发送:

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

我的功能

- (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]];
}

如您所见,我创建了一个字符串并在我的函数中说出这些字符串中的内容,如果我说字符串@“0,22 0,45”是浮点数,我的服务器将只获得第一个数字,那么我如何发送两个浮点数到我的服务器?谢谢你。

4

1 回答 1

0

我没有对此进行测试,甚至没有阅读过 API,但我想你必须创建一个接受类型/值数组的方法版本:

- (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]];
}

注意:当您int在 Objective-C 集合类中传递类型 ( ) 时,它们必须包装在NSNumber对象中:

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

注意 2:对您的方法的改进是传递字符串 as NSString、 numbers/bools asNSNumber和 data asNSData而不是NSString一直使用。不过,由你决定。

于 2014-04-08T14:28:48.923 回答