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
41
Lessons from Mobile Networking at Scale
jpsim
0
60
Bespoke, Artisanal Swift Static Analysis
jpsim
2
1.7k
Performance Profiling Swift on Linux
jpsim
4
1.5k
Realm Mobile Platform Experience
jpsim
3
130
"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
110
Other Decks in Programming
See All in Programming
CSC509 Lecture 08
javiergs
PRO
0
270
Migration to Signals, Resource API, and NgRx Signal Store
manfredsteyer
PRO
0
140
業務でAIを使いたい話
hnw
0
220
NIKKEI Tech Talk#38
cipepser
0
340
Amazon ECS Managed Instances が リリースされた!キャッチアップしよう!! / Let's catch up Amazon ECS Managed Instances
cocoeyes02
0
120
AI 駆動開発におけるコミュニティと AWS CDK の価値
konokenj
5
300
ALL CODE BASE ARE BELONG TO STUDY
uzulla
28
6.9k
GC25 Recap: The Code You Reviewed is Not the Code You Built / #newt_gophercon_tour
mazrean
0
140
TransformerからMCPまで(現代AIを理解するための羅針盤)
mickey_kubo
7
5.8k
Blazing Fast UI Development with Compose Hot Reload (Bangladesh KUG, October 2025)
zsmb
2
440
Vue 3.6 時代のリアクティビティ最前線 〜Vapor/alien-signals の実践とパフォーマンス最適化〜
hiranuma
2
330
コードとあなたと私の距離 / The Distance Between Code, You, and I
hiro_y
0
210
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
174
15k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.1k
Documentation Writing (for coders)
carmenintech
76
5.1k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
Why You Should Never Use an ORM
jnunemaker
PRO
60
9.6k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.2k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
36
6.1k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.6k
Testing 201, or: Great Expectations
jmmastey
46
7.7k
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 }