Giter Site home page Giter Site logo

mindedcoder / bytebackpacker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from michaeldorner/bytebackpacker

0.0 1.0 0.0 94 KB

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

License: MIT License

Objective-C 10.43% Swift 89.57%

bytebackpacker's Introduction

[![Build Status](https://travis-ci.org/michaeldorner/ByteBackpacker.svg)](https://travis-ci.org/michaeldorner/ByteBackpacker) [![codecov.io](https://codecov.io/github/michaeldorner/ByteBackpacker/coverage.svg?branch=master)](https://codecov.io/github/michaeldorner/ByteBackpacker?branch=master) [![codebeat badge](https://codebeat.co/badges/390a34ec-d7ba-4165-bb38-c338247ec04a)](https://codebeat.co/projects/github-com-michaeldorner-bytebackpacker) [![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://badges.mit-license.org)

ByteBackpacker

ByteBackpacker is a small utility written in Swift to pack value types into a Byte array and unpack them back. Additionally, there is a NSData extension to convert NSData objects into a Byte array.

Byte is a typealias for UInt8.

Embedding in projects

  1. Copy the ByteBackpacker.swift file (containing the ByteBackpacker class) to your project.
  2. No second step, you are done.

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.

Usage

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.

Examples

From Double to [Byte] and from [Byte] to Double

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

// either without type inference
let doubleFromByteArray: Double = ByteBackpacker.unpack(byteArray)
/* or */ let doubleFromByteArray = ByteBackpacker.unpack(byteArray) as Double

// or with type inference, but explizit type parameter
let doubleFromByteArray = ByteBackpacker.unpack(byteArray, toType: Double.self)

From Double over NSData to [Byte] and from [Byte] to Double

var aDouble: Double = 1.0
let data = NSData(bytes: &aDouble, length: sizeof(Double.self))
let byteArray = data.toByteArray()
let doubleFromByteArray = ByteBackpacker.unpack(byteArray, toType: Double.self)

API

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

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

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

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

or otherwise, if you want to use type inference

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

Discussion

Unfortunately, there is no suitable option 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>(...), but until today Swift does not provide us this opportunities. We will see what the future brings.

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

Contributions

To-Do

  • Find a solution for making sure, that T is a value type, but not a reference type
  • Add documentation to the source code for a nice Xcode integration
  • Add more test cases
  • Add more examples and how-tos if requested

Acknowledgements

Thanks to Martin R for his suggestions in codereview.stackexchange.com.

License

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.