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
190
Pushing Envoy Beyond the Edge
jpsim
0
21
Lessons from Mobile Networking at Scale
jpsim
0
33
Bespoke, Artisanal Swift Static Analysis
jpsim
2
1.6k
Performance Profiling Swift on Linux
jpsim
4
1.3k
Realm Mobile Platform Experience
jpsim
3
94
"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
90
Other Decks in Programming
See All in Programming
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
Jakarta EE meets AI
ivargrimstad
0
240
距離関数を極める! / SESSIONS 2024
gam0022
0
290
Remix on Hono on Cloudflare Workers
yusukebe
1
300
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
250
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
120
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.2k
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
240
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Designing the Hi-DPI Web
ddemaree
280
34k
Code Review Best Practice
trishagee
64
17k
Thoughts on Productivity
jonyablonski
67
4.3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Teambox: Starting and Learning
jrom
133
8.8k
A better future with KSS
kneath
238
17k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
RailsConf 2023
tenderlove
29
900
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 }