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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
59
Lessons from Mobile Networking at Scale
jpsim
0
78
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
120
Other Decks in Programming
See All in Programming
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
250
20260320登壇資料
pharct
0
140
PHPのバージョンアップ時にも役立ったAST(2026年版)
matsuo_atsushi
0
270
OTP を自動で入力する裏技
megabitsenmzq
0
130
AI時代の脳疲弊と向き合う ~言語学としてのPHP~
sakuraikotone
1
1.7k
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
830
PHPで TLSのプロトコルを実装してみる
higaki_program
0
610
Coding at the Speed of Thought: The New Era of Symfony Docker
dunglas
0
3.6k
我々はなぜ「層」を分けるのか〜「関心の分離」と「抽象化」で手に入れる変更に強いシンプルな設計〜 #phperkaigi / PHPerKaigi 2026
shogogg
2
720
メッセージングを利用して時間的結合を分離しよう #phperkaigi
kajitack
3
520
[PHPerKaigi 2026]PHPerKaigi2025の企画CodeGolfが最高すぎて社内で内製して半年運営して得た内製と運営の知見
ikezoemakoto
0
310
GoのDB アクセスにおける 「型安全」と「柔軟性」の両立 - Bob という選択肢
tak848
0
290
Featured
See All Featured
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
170
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
300
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.2k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.5k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
440
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
64
53k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.4k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
230
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.9k
Navigating Team Friction
lara
192
16k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
180
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 }