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
210
Pushing Envoy Beyond the Edge
jpsim
0
34
Lessons from Mobile Networking at Scale
jpsim
0
50
Bespoke, Artisanal Swift Static Analysis
jpsim
2
1.6k
Performance Profiling Swift on Linux
jpsim
4
1.4k
Realm Mobile Platform Experience
jpsim
3
110
"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
98
Other Decks in Programming
See All in Programming
UPDATEがシステムを複雑にする? イミュータブルデータモデルのすすめ
shimomura
0
210
primeNumberでのRBS導入の現在 && RBS::Traceでinline RBSを拡充してみた
mnmandahalf
0
250
TSConfigからTypeScriptの世界を覗く
planck16
2
1.3k
當開發遇上包裝:AI 如何讓產品從想法變成商品
clonn
0
2.6k
技術懸念に立ち向かい 法改正を穏便に乗り切った話
pop_cashew
0
830
Cloudflare Realtime と Workers でつくるサーバーレス WebRTC
nekoya3
0
240
型付け力を強化するための Hoogle のすゝめ / Boosting Your Type Mastery with Hoogle
guvalif
1
230
External SecretsのさくらProvider初期実装を担当しています
logica0419
0
240
『Python → TypeScript』オンボーディング奮闘記
takumi_tatsuno
1
140
OpenNext + Hono on Cloudflare でイマドキWeb開発スタックを実現する
rokuosan
0
110
Doma で目指す ORM 最適解
nakamura_to
1
160
人には人それぞれのサービス層がある
shimabox
3
460
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
How to Think Like a Performance Engineer
csswizardry
23
1.6k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.8k
Building a Modern Day E-commerce SEO Strategy
aleyda
40
7.3k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
42
2.3k
Speed Design
sergeychernyshev
30
970
4 Signs Your Business is Dying
shpigford
183
22k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
6
660
The Pragmatic Product Professional
lauravandoore
35
6.7k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
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 }