This framework is an assistant in generating binary files. Makes it a lot easier, at least for me.
Its philosophy is essentially to make compiling binary data easier to comprehend, as most of the functionality is already available directly in basic Swift types. Through a combination of typealiases to help mentally bridge the concept of Byte to UInt8, etc and conveniences to append larger types, automatically breaking them down into their smaller components (like taking a Word/UInt32 and breaking it into 4 Byte/UInt8s to be successively appended to the blob). Additionally, it's meant to be as unobtrusive as possible, mimicing existing method naming styles so as to naturally slip into your code. There's more info on my blog.
Note that, while semantically, the terms may not be entirely correct as they are technically relative to the platform they are on, they should should line up with what their typically understood to be. I think. I don't know everything.
So if you have a need to conform to a file spec that outputs binary data, this should help assist in making that easier.
It provides methods to easily convert bidirectionallty between of the following types:
| Type | Alias |
|---|---|
UInt8 |
Byte |
UInt16 |
TwoByte |
UInt32 |
Word |
UInt64 |
LongWord |
All of the following can be converted to any of the above types easily:
UIntIntInt8Int16Int32Int64
And finally, the following can be converted to an array of Byte:
FloatDouble
The integer types all gain the following properties and methods (which enable the above mentioned functionality) through the BinaryFormattingProtocol:
var byteCount: Intstatic var typeByteCount: Intvar longWords: [LongWord]var longWord: LongWordvar wordsArray: [Word](wordsis already part of vanilla Swift)var word: Wordvar twoBytes: [TwoByte]var twoByte: TwoBytevar bytes: [Byte]var byte: Bytevar hexString: Stringvar binaryString: Stringinit(hexString: String)init(character: Character)subscript(_ index: Int) -> UInt8(returns the value of the bit in the requested index)subscript(padded index: Int) -> UInt8(returns the value of the bit in the requested index, padding if you escape thebitWidthof the type)
And both integer AND float types gain the BitRepper protocol, allowing easy retrieval of the underlying bit representation of the value. (Yes, float types come with this, but with the protcol, you can pass either type into a function and get the underlying bitPattern regardless)
So this is great and everything, but what can you do with it? Well, Data is a great type. The thing is, it's a bit strict. Its main interaction is with UInt8s, or Bytes. It only likes appending Bytes, initializing from Bytes, and pretty much nothing else. But that doesn't make much sense, does it? I mean, all these other types are really just UInt8/Bytess underneath it all, aren't they? (Yes, they are) Well, the real magic of SwiftyBinaryFormatter is that you now can use all these types with Data and it will magically just break them down into their source Bytes for you.
So, have an Int you need to store in a Data blob? Easy!
let myInt = 1234
let myFirstData = Data(myInt.bytes)Wait, now I need to smash a magic number on the front, and follow it up with a Double value of today's date and then pi.:
var compiledData = Data(magicNumber)
compiledData.append(myFirstData)
compiledData.append(Date().timeIntervalSince1970)
compiledData.append(Double.pi)And finally add a Byte, Word, TwoByte, Word, LongWord sequence because reasons.
compiledData.append(contentsOf: [Byte(3), Word(42), TwoByte(1238), Word(123456789), LongWord(9999999999)])I think you get the picture.
- Add
github "mredig/SwiftyBinaryFormatter" ~> 1.0.0to your cartfile carthage update- Add
import SwiftyBinaryFormatterto the top of any file you with to use it in
- Add the line
.package(url: "https://github.com/mredig/SwiftyBinaryFormatter.git", from: "1.0.0")to the appropriate section of yourPackage.swift- The Package Name is
SwiftyBinaryFormatter- add that as a dependency to any targets you want to use it in.
- The Package Name is
swift package updateor use Xcode- Add
import SwiftyBinaryFormatterto the top of any file you with to use it in
- Run
brew install carthage - Create a file in your project called
Cartfileand add the linegithub "mredig/SwiftyBinaryFormatter" ~> 1.0.0 - Run
carthage update - ...Basically, just use Carthage. It's better.