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
1.3k
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
72
Lessons from Mobile Networking at Scale
jpsim
0
97
Bespoke, Artisanal Swift Static Analysis
jpsim
2
1.8k
Performance Profiling Swift on Linux
jpsim
4
1.6k
Realm Mobile Platform Experience
jpsim
3
160
"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
150
Other Decks in Programming
See All in Programming
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
560
AIエージェントで 変わるAndroid開発環境
takahirom
2
740
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
290
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
430
Go言語とトイモデルで学ぶTransformerの気持ち / fukuokago23-transformer
monochromegane
0
150
テーブルをDELETEした
yuzneri
0
130
AIが無かった頃の素敵な出会いの話
codmoninc
1
300
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
190
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
290
人間の目はかわらない、だからJPEGは30年もつ
yuzneri
12
17k
これって Effect でできたのでは? / TSKaigi Mashup Kansai #2
susisu
0
110
言語を使う側から、作る側へ。 自作 Lisp で得た新たな気づき。
andpad
0
140
Featured
See All Featured
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
470
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
430
Side Projects
sachag
455
43k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.9k
For a Future-Friendly Web
brad_frost
183
10k
Automating Front-end Workflow
addyosmani
1370
210k
Into the Great Unknown - MozCon
thekraken
41
2.6k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
330
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 }