Single-value-tuple as last member of struct in swift
Asked Answered
L

2

9

MusicPlayer's API relies on variable length arrays as the last member of a struct to handle passing around data of unknown size. Looking at the generated interface for MusicPlayer, the structs used in this method present their last element in a single value tuple.

example:

struct MusicEventUserData {
    var length: UInt32
    var data: (UInt8)
}

I doubt that any of this has been officially exposed but has anyone figured out whether this syntax is a red herring or actually significant? I don't think that there is a means to hand arbitrarily sized things via swift but does this help when calling from C?

Labaw answered 26/1, 2015 at 4:16 Comment(1)
Here is a similar issue #27724555 with a possible solution (or workaround). I do hope that this will become easier in the future.Brauer
L
0

MusicPlayer is no longer exported as above. As of Xcode 6.3b1

typedef struct MusicEventUserData
{
    UInt32      length;
    UInt8       data[1];
} MusicEventUserData;

This is much closer to the C declaration. It still does not completely explain how to deal with the API in swift but that is another question.

Labaw answered 17/2, 2015 at 15:50 Comment(0)
O
1

after test on a playground I can see there is no difference between (Int) and Int type. Here is my tests :

func testMethod(param1: Int, param2: (Int)) -> Int{
    return param1 + param2
}

testMethod(2, 3) // return 5
testMethod(3, (6)) // return 9

About the calling in C, I just think it is a little bug on the bridging from ObjC to swift

Oscillator answered 16/2, 2015 at 1:39 Comment(1)
I understand that. The question was about the fact that the single element notation lined up with a C idiom. There weren't any other examples of Apple generating types with that syntax and it always lined up with the variable length structs.Labaw
L
0

MusicPlayer is no longer exported as above. As of Xcode 6.3b1

typedef struct MusicEventUserData
{
    UInt32      length;
    UInt8       data[1];
} MusicEventUserData;

This is much closer to the C declaration. It still does not completely explain how to deal with the API in swift but that is another question.

Labaw answered 17/2, 2015 at 15:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.