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
Swift-idl
Search
matuyuji
July 21, 2015
Programming
0
550
Swift-idl
Swift source code generator from Swift
matuyuji
July 21, 2015
Tweet
Share
More Decks by matuyuji
See All by matuyuji
Emacs × Touch Bar
matuyuji
2
1.7k
ARKit + SceneKitでMinesweeperを作ってみた
matuyuji
1
770
Go + QtでiOS アプリ開発
matuyuji
0
370
@_specialized なお話し
matuyuji
0
470
Xcode Souce Code Extensionを使ってみた
matuyuji
0
340
Codebeatを 試してみた
matuyuji
0
730
React Nativeで UIコンポーネントをつくる
matuyuji
0
980
React Nativeを使ってみた
matuyuji
0
1.3k
SwiftでLens
matuyuji
1
940
Other Decks in Programming
See All in Programming
RWC 2024 DICOM & ISO/IEC 2022
m_seki
0
200
14 Years of iOS: Lessons and Key Points
seyfoyun
1
770
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
300
命名をリントする
chiroruxx
1
380
バグを見つけた?それAppleに直してもらおう!
uetyo
0
170
Zoneless Testing
rainerhahnekamp
0
120
コンテナをたくさん詰め込んだシステムとランタイムの変化
makihiro
1
120
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
From Translations to Multi Dimension Entities
alexanderschranz
2
130
Refactor your code - refactor yourself
xosofox
1
260
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | droidcon Italy
prof18
0
150
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
640
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Why Our Code Smells
bkeepers
PRO
335
57k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
VelocityConf: Rendering Performance Case Studies
addyosmani
326
24k
It's Worth the Effort
3n
183
28k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.3k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
How to train your dragon (web standard)
notwaldorf
88
5.7k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
Transcript
swift-idl @matuyuji ؔϞόΠϧΞϓϦݚڀձ #4 2015.7.21
@matuyuji safx-dev.blogspot.jp
JSON Libraries • Himotoki • Decodable • Argo • JSONHelper
• ObjectMapper
Decodable struct FileEntry { let fileName: String let size: Int
} extension FileEntry: Decodable { static func decode(j: AnyObject) throws -> FileEntry { return try Repository( fileName: j => “file-name", size: j => "size", ) } }
• DRYత͡Όͳ͍ • JSONϥΠϒϥϦ͕ཉ͍͠Θ͚͡Όͳ͍
Golang type FileEntry struct { FileName string `json:"file-name"` Size int
`json:"size"` }
ཉ͍͠ͷ • REST APIΫϥΠΞϯτΛָʹߏங͍ͨ͠ • DRYతͳͷ
safx/swift-idl struct FileEntry: JSONDecodable, JSONEncodable, Printable { let fileName: String
// json:"file-name" let size : Int } python swift-idl.py MyProj.xcodeproj
public struct FileEntry: JSONDecodable, JSONEncodable, CustomStringConvertible { public let fileName:
String // json:"file-name" public let size : Int public static func parseJSON(data: AnyObject) throws -> FileEntry { if !(data is NSDictionary) { throw JSONDecodeError.TypeMismatch(key: "FileEntry", type: "NSDictionary") } let fileName: String if let v: AnyObject = data["file-name"] { if v is NSNull { throw JSONDecodeError.NonNullablle(key: "file-name") } else { do { fileName = try String.parseJSON(v) } catch JSONDecodeError.ValueTranslationFailed { throw JSONDecodeError.TypeMismatch(key: "file-name", type: "String") } } } else { throw JSONDecodeError.MissingKey(key: "file-name") } let size: Int if let v: AnyObject = data["size"] { if v is NSNull { throw JSONDecodeError.NonNullablle(key: "size") } else { do { size = try Int.parseJSON(v) } catch JSONDecodeError.ValueTranslationFailed { throw JSONDecodeError.TypeMismatch(key: "size", type: "Int") }
Protocols protocol Printable {} protocol NSCoding {} protocol JSONEncodable {}
protocol JSONDecodable {} protocol URLRequestHelper {} protocol ClassInit {} protocol EnumStaticInit {}
URLRequestHelper enum Router: URLRequestHelper { case GetProfile // router:",profile" case
GetMessages(topicId: TopicID, // router:"GET,topics/\(topicId)" count: Int?) case PostMessage(topicId: TopicID, // router:"POST,topics/\(topicId)" message: String, replyTo: Int?) }
public enum Router { case GetProfile case GetMessages(topicId: TopicID, count:
Int?) case PostMessage(topicId: TopicID, message: String, replyTo: Int?) public var method: String { switch self { case .GetProfile: return "GET" case .GetMessages: return "GET" case .PostMessage: return "POST" } } public var path: String { switch self { case .GetProfile: return "profile" case .GetMessages(let (topicId, _)): return "topics/\(topicId)" case .PostMessage(let (topicId, _, _)): return "topics/\(topicId)" } } public var params: [String: AnyObject] { switch self { case .GetProfile: return [:] case .GetMessages(let (_, count)): var p: [String: AnyObject] = [:] count.map { p["count"] = $0.toJSON() } return p case .PostMessage(let (_, message, replyTo)): var p: [String: AnyObject] = ["message": message.toJSON()] replyTo.map { p["replyTo"] = $0.toJSON() } return p } } }
Install & Setting • brew install sourcekitten • github clone
https://github.com/safx/swift-idl.git TypetalkKit swift-2.0 Xcode 6.4 Xcode 7