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
150
Pushing Envoy Beyond the Edge
jpsim
0
13
Lessons from Mobile Networking at Scale
jpsim
0
25
Bespoke, Artisanal Swift Static Analysis
jpsim
2
1.5k
Performance Profiling Swift on Linux
jpsim
4
1.2k
Realm Mobile Platform Experience
jpsim
3
82
"Watch Your Language!": The road to clean code with SwiftLint
jpsim
6
77k
Mastering Realm Notifications
jpsim
1
34k
Realm 1.0 Party
jpsim
1
87
Other Decks in Programming
See All in Programming
仕様と実装で学ぶOpenTelemetry
drumato
2
1.3k
An adventure of Happy Eyeballs
coe401_
1
310
TypeScript Custom GitHub Action Development Tips
peaceiris
5
880
[RubyKaigi 2024] Ruby Mixology 101: adding shots of PHP, Elixir, and more
palkan
0
140
Go製Webアプリケーションのエラーとの向き合い方大全、あるいはやっぱりスタックトレース欲しいやん / Kyoto.go #50
utgwkk
6
2k
『WordPressコミュニティで学ぶ』OSS貢献の多様性
ippey
0
300
RubyGems on ruby.wasm
kateinoigakukun
0
160
WinActorの勉強を継続する方法
tamai_63
0
130
Deep Dive into React Stream/Serialize
mugi_uno
4
880
slow types ってなんだろう?
karad
0
210
Three ways to use AI on Android: The Good, the Bad and the Ugly
marxallski
0
120
GitHub Actionsの痒いところを埋めるサードパーティーランナー
dora1998
2
290
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
261
12k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
104
6.7k
Building an army of robots
kneath
300
42k
Designing for Performance
lara
601
67k
Git: the NoSQL Database
bkeepers
PRO
423
63k
Mobile First: as difficult as doing things right
swwweet
217
8.6k
GraphQLとの向き合い方2022年版
quramy
33
13k
Principles of Awesome APIs and How to Build Them.
keavy
121
16k
Design by the Numbers
sachag
274
18k
Facilitating Awesome Meetings
lara
43
5.6k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
245
20k
The MySQL Ecosystem @ GitHub 2015
samlambert
244
12k
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 }