Swiftpack.co - Package - michaeldorner/ByteBackpacker

Swift Build Status codecov codebeat badge CocoaPods CocoaPods MIT licensed Swift Package Manager compatible Average time to resolve an issue Percentage of issues still open


ByteBackpacker is a small utility written in pure Swift to pack value types into a Byte¹ array and unpack them back.

Additionally, there is a Data (formerly NSData) extension to convert Data objects into a Byte array.

¹ Byte is a typealias for UInt8.

Table of Contents


You have three options:

  1. Copy the ByteBackpacker.swift file (containing the ByteBackpacker class) to your project.

  2. Use Swift Package Manager:

     import PackageDescription
     let package = Package(
         name: "TestImport",
         dependencies: [
             .Package(url: "https://github.com/michaeldorner/ByteBackpacker.git")
  3. Use Cocoapods: pod 'ByteBackpacker'

Although it can be used in the same way in Objective-C, I had clearly Swift projects in mind. The easiest way for Objective-C users is to embed the ByteBackpacker.framework. Of course, Swift users can also do this, but actually I do not see any advantages.


Important for a proper usage: ByteBackpacker does only support value types (e.g. numbers, structs, ...), but no reference types (e.g. classes)! For further information see Discussion.

All examples can be seen running in the ByteBackpackerPlayground.playground. Let's have a look on some general use cases:

From Double to [Byte]

let aDouble: Double = 1.0
let aByteArray: [Byte] = ByteBackpacker.pack(aDouble)

From [Byte] to Double

let option_1: Double = ByteBackpacker.unpack(aByteArray)
let option_2 = ByteBackpacker.unpack(aByteArray) as Double
let option_3 = ByteBackpacker.unpack(aByteArray, toType: Double.self)

From Double to Data to [Byte] to Double

var anotherDouble: Double = 2.0
let data = Data(bytes: &anotherDouble, count: MemoryLayout<Double>.size)
var byteArrayFromNSData = data.toByteArray()
let doubleFromByteArray = ByteBackpacker.unpack(byteArrayFromNSData, toType: Double.self)

From [Byte] to Data

let anotherByteArray: [Byte] = [0, 0, 0, 0, 0, 0, 8, 64]
let dataFromByteArray = Data(bytes: anotherByteArray)


Byte is a typealias for UInt8.

ByteOrder is an enum for Little Endian and Big Endian. Furthermore, there is the option for asking the platform you are using for the native byte order of the system: ByteOrder.nativeByteOrder. By default .nativeByteOrder is applied for packing and unpacking.

For packing value types into a [Byte], use

open class func pack<T: Any>( _ value: T, byteOrder: ByteOrder = .nativeByteOrder) -> [Byte]

For unpacking a [Byte] into a value type, use either

open class func unpack<T: Any>(_ valueByteArray: [Byte], byteOrder: ByteOrder = .nativeByteOrder) -> T

or otherwise, if you want to use type inference

open class func unpack<T: Any>(_ valueByteArray: [Byte], toType type: T.Type, byteOrder: ByteOrder = .nativeByteOrder) -> T



There is no way for specifying value types in Swift's generics (see here the discussion on stackoverflow). It would be awesome to specify our methods like func (un)pack<T: Any where T: ~AnyClass>(...) to let the compiler check for value types. So far ByteBackpacker ensures the value type with assert(...).

An open question is how to test the assert(...) for this value type check.

I would love to improve this project. Tell me your ideas, here in github, via mail or in codereview.stackexchange.com.



  • [x] Find a solution for making sure, that T is a value type, but not a reference type
  • [x] Add documentation to the source code for a nice Xcode integration
  • [ ] Find a solution for testing assert()


Many thanks to


(Un)fortunately there is a lot of work going on Swift. This made larger changes to ByteBackpacker needed. The following table shows the compatibility.

| Swift version | 2.0 | 3.0 | 4.0 and later | :-: | :-: | :-: | :-: | | ByteBackpacker 1.0 | ✓ | ✗ | ✗ | ByteBackpacker 1.1 | ✗ | ✓ | ✗ | ByteBackpacker 1.2 and later | ✗ | ✓ | ✓

Hopefully the APIs will be stable now.


ByteBackpacker is released under the MIT license. See LICENSE for more details.


Stars: 18
Help us keep the lights on


Used By

Total: 0


1.2.1 - May 23, 2018

Fixed version issue with Cocoapods (thanks for the hint by @sashahilton00)

1.2 - Oct 27, 2017

  • updated to Swift 4
  • updated to Xcode 8.1
  • fixed some minor issues in the README.MD

1.1.2 - Dec 22, 2016

Swift Package Manager Support

1.1.1 - Nov 11, 2016

  • added reference type check
  • updated to Xcode 8.1
  • restructured test cases

1.1 - Sep 23, 2016

ByteBackpacker now supports Swift 3 and Xcode 8 (thanks to iCodist).

Known problems

  • Reference type check does not work as we did it in Swift 1/2.

As soon this problem is solved, we remove the pre-release status.