Skip to content

Instantly share code, notes, and snippets.

@langford
Last active August 16, 2018 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save langford/dac981d8fe1291a593f627faae643594 to your computer and use it in GitHub Desktop.
Save langford/dac981d8fe1291a593f627faae643594 to your computer and use it in GitHub Desktop.
Chunking data
//: Playground - noun: a place where people can play
import UIKit
struct ChunkedData{
let chunkCount:Int
let chunkSize:Int
let byteCount:Int
let originalData:Data
let chunks:[Data]
init(data:Data,chunkSizeInBytes:Int){
self.originalData = data
self.byteCount = data.count
self.chunkSize = chunkSizeInBytes
self.chunkCount = byteCount/chunkSize
var chunksForData:[Data] = []
//todo
self.chunks = chunksForData
}
}
struct ChunkMessage{
let chunk:Data
let index:Int
}
///locate this in your bluetooth communication file
extension Data{
//Used to send a specific chunk
func sendChunk(to:String,chunkIndexMetadata:Int){
let msg = ChunkMessage(chunk:self, index:chunkIndexMetadata)
//implement bluetooth send
}
}
///locate this in hyour bluetooth communication file
extension ChunkedData{
func resendChunks(to destination:String,chunkIndicies:[Int]){
for i in chunkIndicies{
chunks[i].sendChunk(to: destination, chunkIndexMetadata: i)
}
}
}
let s = "aoisdjofaidsjfsfdijfoaisjfdoafisjdfoiajsfosdijfaosidfjsoafijoifj"
let data = s.data(using: .utf8)!
let chunks = ChunkedData(data: data, chunkSizeInBytes: 10).chunks
chunks.map { chunk in
let string = String(decoding: chunk, as: UTF8.self)
print("\(string) is a chunk")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment