Using terms in Swift

Now that we've reached Swift 2.0, I've decided to convert my unfinished OS X app to Swift. Progress, but I ran into some problems using the terms and could use some clarification and advice.

A thermal structure is seen as a structure in Swift, and not surprisingly, it is surprising that the control array in the structure is now a tuple. I expected it to be just an array. As you might imagine, it took me a while to figure this out. Work in a playground if I do:

var settings:termios = termios()
print(settings)

      

then I get the correct data printed for the structure.

In Obj-C, to set control characters, you must use, say,

cfmakeraw(&settings);
settings.c_cc[VMIN] = 1;

      

where VMIN

is the #define equal to 16 in termios.h. In Swift, I need to do

cfmakeraw(&settings)
settings.c_cc.16 = 1

      

which works, but is a little more opaque. I would rather use something line by line

settings.c_cc.vim = 1

      

instead, but can't seem to find any documentation describing the Swift version of termios. Does anyone know if a tuple has pre-assigned names for it or not, is there a way to assign names after the fact? Should I just create my own tuple with named elements and then assign it settings.c_cc

?

Interestingly, even though preprocessor directives shouldn't work in Swift, if I do

print(VMIN)
print(VTIME)

      

then the correct values ​​are printed and no compiler errors are generated. I would be interested in any clarification or comments on this matter. This is mistake?

The rest of the problems are related to the further configuration of termios.

The definition cfsetspeed

is given as

func cfsetspeed(_: UnsafeMutablePointer<termios>, _: speed_t) -> Int32

      

and speed_t

typedef'ed as unsigned long. In Obj-C we would do

cfsetspeed(&settings, B38400);

      

but since there B38400

is a #define in termios.h, we can no longer do that. Apple has installed a global constant replacement for things like this in Swift, and if so, can someone tell me where they are documented. The alternative is to simply plug in the raw values ​​and lose readability, or create your own versions of the constants previously defined in termios.h. I am happy to go this route if there is no better choice.

+3


source to share


1 answer


Let's start with your second problem, which is easier to solve. B38400

available in Swift, it is of the wrong type. Therefore, you must explicitly convert it:

var settings = termios()
cfsetspeed(&settings, speed_t(B38400))

      


Your first problem does not have a "nice" solution that I know of. Fixed size arrays are imported into Swift as tuples, and as far as I know, you cannot address a tuple element with a variable.

You can do pointer manipulation and take advantage of the fact that the address of a tuple can be thought of as the address of an array of elements, as in Convert String to int8 array :



var settings = termios()
withUnsafeMutablePointer(&settings.c_cc) { (tuplePtr) -> Void in
    let ccPtr = UnsafeMutablePointer<cc_t>(tuplePtr)
    ccPtr[Int(VMIN)] = 1
}

      

But not that converting that tuple to an array is not actually documented, so in this case you might want to keep

settings.c_cc.16 = 1

      

with an integer literal 16

.

+2


source







All Articles