Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Working With Binary Data in Swift
Search
JP Simard
October 29, 2015
Programming
4
1.2k
Working With Binary Data in Swift
Presented at Swift Summit SF 2015.
Source available here:
https://github.com/jpsim/talks
JP Simard
October 29, 2015
Tweet
Share
More Decks by JP Simard
See All by JP Simard
Unconventional Swift Patterns
jpsim
0
3.4k
Pushing Envoy Beyond the Edge
jpsim
0
40
Lessons from Mobile Networking at Scale
jpsim
0
57
Bespoke, Artisanal Swift Static Analysis
jpsim
2
1.7k
Performance Profiling Swift on Linux
jpsim
4
1.5k
Realm Mobile Platform Experience
jpsim
3
120
"Watch Your Language!": The road to clean code with SwiftLint
jpsim
6
78k
Mastering Realm Notifications
jpsim
1
34k
Realm 1.0 Party
jpsim
1
100
Other Decks in Programming
See All in Programming
為你自己學 Python - 冷知識篇
eddie
1
350
AIと私たちの学習の変化を考える - Claude Codeの学習モードを例に
azukiazusa1
11
4.4k
パッケージ設計の黒魔術/Kyoto.go#63
lufia
3
440
CloudflareのChat Agent Starter Kitで簡単!AIチャットボット構築
syumai
2
510
時間軸から考えるTerraformを使う理由と留意点
fufuhu
16
4.8k
250830 IaCの選定~AWS SAMのLambdaをECSに乗り換えたときの備忘録~
east_takumi
0
400
print("Hello, World")
eddie
2
530
How Android Uses Data Structures Behind The Scenes
l2hyunwoo
0
480
アルテニア コンサル/ITエンジニア向け 採用ピッチ資料
altenir
0
110
Zendeskのチケットを Amazon Bedrockで 解析した
ryokosuge
3
320
Updates on MLS on Ruby (and maybe more)
sylph01
1
180
複雑なフォームに立ち向かう Next.js の技術選定
macchiitaka
2
240
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
850
Designing for Performance
lara
610
69k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
530
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Gamification - CAS2011
davidbonilla
81
5.4k
Side Projects
sachag
455
43k
Docker and Python
trallard
46
3.6k
Agile that works and the tools we love
rasmusluckow
330
21k
Context Engineering - Making Every Token Count
addyosmani
3
62
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Documentation Writing (for coders)
carmenintech
74
5k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Transcript
Working with Binary Data in Swift
JP Simard @ Realm
None
Options → NSData → [UInt8] → withUnsafePointer() → ???
Layout-Aligned Structs
func encode<T>(var value: T) -> NSData { return withUnsafePointer(&value) {
p in NSData(bytes: p, length: sizeofValue(value)) } } func decode<T>(data: NSData) -> T { let pointer = UnsafeMutablePointer<T>.alloc(sizeof(T)) data.getBytes(pointer, length: sizeof(T)) return pointer.move() }
enum Either<T> { case Left(T) case Right(T) } let value
= Either.Left("Swift Summit") let data = encode(value) data // => <NSData> let decoded: Either<String> = decode(data) decoded // => Either.Left("Swift Summit")
None
None
Get SourceKit syntax map struct A { subscript(index: Int) ->
() { return () } }
Result 00 00 00 00 00 00 00 00 30
00 00 00 00 00 00 00 40 4c 81 01 01 00 00 00 00 00 00 00 0c 00 00 00 78 4c 81 01 01 00 00 00 07 00 00 00 14 00 00 00 b0 4c 81 01 01 00 00 00 12 00 00 00 1e 00 00 00 00
Result 00 00 00 00 00 00 00 00 30
00 00 00 00 00 00 00 --------16 bytes------- --------16 bytes------- 40 4c 81 01 01 00 00 00 00 00 00 00 0c 00 00 00 --------16 bytes------- --------16 bytes------- 78 4c 81 01 01 00 00 00 07 00 00 00 14 00 00 00 --------16 bytes------- --------16 bytes------- b0 4c 81 01 01 00 00 00 12 00 00 00 1e 00 00 00 --------16 bytes------- --------16 bytes------- 00
!
struct SyntaxToken { let type: String let offset: Int let
length: Int }
Strideable
tokens = 16.stride(through: numberOfTokens * 16, by: 16).map { parserOffset
in . }
tokens = 16.stride(through: numberOfTokens * 16, by: 16).map { parserOffset
in var uid = UInt64(0), offset = 0, length = 0 data.getBytes(&uid, range: NSRange(location: parserOffset, length: 8)) data.getBytes(&offset, range: NSRange(location: 8 + parserOffset, length: 4)) data.getBytes(&length, range: NSRange(location: 12 + parserOffset, length: 4)) }
tokens = 16.stride(through: numberOfTokens * 16, by: 16).map { parserOffset
in var uid = UInt64(0), offset = 0, length = 0 data.getBytes(&uid, range: NSRange(location: parserOffset, length: 8)) data.getBytes(&offset, range: NSRange(location: 8 + parserOffset, length: 4)) data.getBytes(&length, range: NSRange(location: 12 + parserOffset, length: 4)) return SyntaxToken( type: stringForSourceKitUID(uid) ?? "unknown", offset: offset, length: length >> 1 ) }
Collection of Bytes → Making our own → Conforming to
ExtensibleCollectionType → What Index type should we use? Int?
Just end up with [UInt8]
Links → SourceKittenFramework SyntaxMap → Convert structs and enums to
NSData → robnapier.net/nsdata → Simon Lewis on parsing OLE/COM → github.com/realm/jazzy → realm.io
try! ask(...) struct Question { let value: String let canJPAnswer:
Bool } func ask<S: SequenceType where S.Generator.Element == Question>(questions: S) throws { // excercise for attendees }