Swift Data Decoder. Easily Decode Data values
DataDecoder is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod"DataDecoder"Swift Package Manager:
dependencies:[.package(url:"https://github.com/FitnessKit/DataDecoder", from:"5.0.0"),]Swift4
dependencies:[.package(url:"https://github.com/FitnessKit/DataDecoder",.branch("swift42")),]Example:
letsensorData:Data=Data([0x02,0xFE,0xFF,0xEF,0xBE,0xAD,0xDE,0xA5])vardecoder=DecodeData()letheight= decoder.decodeUInt8(sensorData)letweight= decoder.decodeUInt16(sensorData)letdeadbeef= decoder.decodeUInt32(sensorData)letnib= decoder.decodeNibble(sensorData)
//This should come back 0 as there is no more data left
letnovalue= decoder.decodeNibble(sensorData)Example Using Optionals:
letsensorData:Data=Data([0x02,0xFE,0xFF,0xEF,0xBE,0xAD,0xDE,0xA5])vardecoder=DecodeData()iflet height = decoder.decodeUInt8IfPresent(sensorData){}letweight= decoder.decodeUInt16IfPresent(sensorData)letdeadbeef= decoder.decodeUInt32IfPresent(sensorData)letnib= decoder.decodeNibbleIfPresent(sensorData)
// This will be nil as there is no data left
letnovalue= decoder.decodeUInt8IfPresent(sensorData)- Nibble
- UInt8/Int8
- UInt16/Int16
- UInt24/Int23
- UInt32/Int32
- UInt48
- UInt64/Int64
- IP Address to String Value
- MAC Address to String Value
- 16-bit SFLOAT
- 32-bit FLOAT
- Float32
- Float64
This package is developed and maintained by Kevin A. Hoogheem
DataDecoder is available under the MIT license