I am writing a MacOS application that reads in a binary data file. The file is broke up into two sections.
The first section of the file lists all the information about each channel in the file. For example the channel name, the unit of measure, the type of the data. The possible types are know in advance. For example a channel could be UInt8, Int16, float, etc.
The second part of the file is the data. Each set of points starts with a timestamp followed by all of the channel values for that sample in the same order that they are in the header. This pattern repeats until the end of the file.
I have a class model that will store all of the information about the channel along with an array containing all of the data for that channel.
My question is how could I approach the array for the data in each channel. I won't know in advance what type each channel is until I read the file. I could have an array of each possible type in the model but that feels like a bit of a waste. Is there any way I could either create the array at runtime to the proper type or change the array type at runtime?
I am using swift 5.
If you don't know the types of the array's elements in the "channel" at compile-time, then I would recommend that you convert them to the widest number type among all of the possible types in the channel. For example, if the channel could contain Int32 or Int64 number types, then your array in Swift should always contain elements of type Int64, because this is the wider type. Just convert values of type Int32 to Int64 when initializing the Swift array from the channel data.