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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
JP Simard
October 29, 2015
Programming
1.3k
4
Share
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
More Decks by JP Simard
See All by JP Simard
Unconventional Swift Patterns
jpsim
0
3.5k
Pushing Envoy Beyond the Edge
jpsim
0
62
Lessons from Mobile Networking at Scale
jpsim
0
83
Bespoke, Artisanal Swift Static Analysis
jpsim
2
1.7k
Performance Profiling Swift on Linux
jpsim
4
1.5k
Realm Mobile Platform Experience
jpsim
3
150
"Watch Your Language!": The road to clean code with SwiftLint
jpsim
6
79k
Mastering Realm Notifications
jpsim
1
34k
Realm 1.0 Party
jpsim
1
130
Other Decks in Programming
See All in Programming
Xdebug と IDE による デバッグ実行の仕組みを見る / Exploring-How-Debugging-Works-with-Xdebug-and-an-IDE
shin1x1
0
380
年間50登壇、単著出版、雑誌寄稿、Podcast出演、YouTube、CM、カンファレンス主催……全部やってみたので面白さ等を比較してみよう / I’ve tried them all, so let’s compare how interesting they are.
nrslib
4
780
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
330
Coding at the Speed of Thought: The New Era of Symfony Docker
dunglas
0
5k
Going Multiplatform with Your Android App (Android Makers 2026)
zsmb
2
420
SkillがSkillを生む:QA観点出しを自動化した
sontixyou
6
3.4k
t *testing.T は どこからやってくるの?
otakakot
1
630
2026-03-27 #terminalnight 変数展開とコマンド展開でターミナル作業をスマートにする方法
masasuzu
0
340
AI時代のエンジニアリングの原則 / Engineering Principles in the AI Era
haru860
0
290
Kubernetes上でAgentを動かすための最新動向と押さえるべき概念まとめ
sotamaki0421
3
500
PDI: Como Alavancar Sua Carreira e Seu Negócio
marcelgsantos
0
120
ハーネスエンジニアリングとは?
kinopeee
10
5.1k
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
RailsConf 2023
tenderlove
30
1.4k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
450
Marketing to machines
jonoalderson
1
5.2k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.2k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.5k
Being A Developer After 40
akosma
91
590k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
180
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
69
39k
Automating Front-end Workflow
addyosmani
1370
200k
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 }