Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Working With Binary Data in Swift
JP Simard
October 29, 2015
Programming
4
1.1k
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
Bespoke, Artisanal Swift Static Analysis
jpsim
2
1.3k
Performance Profiling Swift on Linux
jpsim
4
1k
Realm Mobile Platform Experience
jpsim
3
64
"Watch Your Language!": The road to clean code with SwiftLint
jpsim
6
69k
Mastering Realm Notifications
jpsim
1
32k
Realm 1.0 Party
jpsim
1
78
SourceKit and You
jpsim
8
17k
Practical Cross-Platform Swift
jpsim
16
21k
A look into Realm's Core DB engine
jpsim
2
23k
Other Decks in Programming
See All in Programming
オブジェクト指向で挫折する初学者へ
deepoil
0
160
【Scrum Fest Osaka 2022】スクラムチームに放り込まれた若手エンジニアの皆さん、どのように技術のキャッチアップをしていくかイメージはついていますか?
miiiki
0
110
Java アプリとAWS の良い関係 - AWS でJava アプリを実行する一番簡単な方法教えます / AWS for Javarista
kanamasa
2
1.2k
Jetpack Composeでの画面遷移
iwata_n
0
170
Modern Android Developer ~ 안내서
pluu
1
640
Gitlab CIでMRを自動生成する
forcia_dev_pr
0
110
Vite でお手軽 Vue.js の環境構築
azuki
2
180
Amazon Aurora の v1 が EOL になるので 10 クラスタアップグレードして出てきたノウハウ
dekokun
0
860
Running Laravel/PHP on AWS (AWS Builders Day Taiwan 2022)
dwchiang
0
140
From Java through Scala to Clojure
lagenorhynque
0
220
無限スクロールビューライブラリ 二つの設計思想比較
harumak
0
240
Jetpack Compose best practices 動画紹介 @GoogleI/O LT会
takakitojo
0
330
Featured
See All Featured
Web Components: a chance to create the future
zenorocha
303
40k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
10
3.4k
Testing 201, or: Great Expectations
jmmastey
21
5.4k
WebSockets: Embracing the real-time Web
robhawkes
57
5.2k
Build The Right Thing And Hit Your Dates
maggiecrowley
19
1.2k
The World Runs on Bad Software
bkeepers
PRO
57
5.3k
Producing Creativity
orderedlist
PRO
334
37k
Done Done
chrislema
174
14k
Building a Scalable Design System with Sketch
lauravandoore
448
30k
Navigating Team Friction
lara
175
11k
Building Flexible Design Systems
yeseniaperezcruz
310
34k
Building Better People: How to give real-time feedback that sticks.
wjessup
344
17k
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 }