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 Macros 紹介
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
swiftty
October 17, 2023
Technology
0
700
個人開発アプリでの Swift Macros 紹介
【ハイブリット開催】Mobile勉強会 Wantedly × チームラボ #11
https://wantedly.connpass.com/event/297601/
で発表した資料です
swiftty
October 17, 2023
Tweet
Share
More Decks by swiftty
See All by swiftty
Wantedly Visit での SKIE の導入と振り返り
swiftty
0
75
iOS 18 から追加された SwiftUI の傾向について調べてみる
swiftty
3
960
Flutter アプリのリリースフローを考える
swiftty
0
370
Meet BrowserEngineKit
swiftty
0
2k
Cloud-managed certificates へ移行した話
swiftty
0
760
同じアプリを Flutter と SwiftUI で書いてみる
swiftty
0
620
Swift Package Manager へ移行した話
swiftty
0
800
SwiftLint にコントリビュートする
swiftty
1
4.7k
SwiftUI でリスト要素のインプレッションを計測する
swiftty
0
630
Other Decks in Technology
See All in Technology
EventBridge API Destination × AgentCore Runtimeで実現するLambdaレスなイベント駆動エージェント
har1101
6
230
それぞれのペースでやっていく Bet AI / Bet AI at Your Own Pace
yuyatakeyama
1
490
エンジニアとして長く走るために気づいた2つのこと_大賀愛一郎
nanaism
1
230
AI時代のPMに求められるのは 「Ops」と「Enablement」
shimotaroo
1
300
持続可能な開発のためのミニマリズム
sansantech
PRO
3
520
2026/01/16_実体験から学ぶ 2025年の失敗と対策_Progate Bar
teba_eleven
1
220
AI時代にあわせたQA組織戦略
masamiyajiri
1
790
さくらのクラウドでのシークレット管理を考える/tamachi.sre#2
fujiwara3
1
210
BPaaSオペレーション・kubell社内 n8n活用による効率化検証事例紹介
kentarofujii
0
270
CodeRabbit CLI + Claude Codeの連携について
oikon48
1
600
Hardware/Software Co-design: Motivations and reflections with respect to security
bcantrill
1
240
Data Intelligence on Lakehouse Paradigm
scotthsieh825
0
190
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
40
2.2k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
280
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
140
Heart Work Chapter 1 - Part 1
lfama
PRO
5
35k
RailsConf 2023
tenderlove
30
1.3k
New Earth Scene 8
popppiees
1
1.4k
Optimising Largest Contentful Paint
csswizardry
37
3.6k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
55
How STYLIGHT went responsive
nonsquared
100
6k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
Transcript
© 2023 Wantedly, Inc. 個人開発アプリでの Swift Macros 紹介 Mobile勉強会 Wantedly
× チームラボ #11 Oct. 17 2023 - 林達也
自己紹介 © 2023 Wantedly, Inc.
Agenda 1. Swift Macros とは 2. 開発中のアプリでの利用例 ◦ member ◦
peer ◦ accessor 3. 使ってみた感想 © 2023 Wantedly, Inc.
Swift Macros とは © 2023 Wantedly, Inc.
Swift Macros • ソースコードのコンパイル時にコードを変換し、繰り返し書く手 間を省く • Swift では 2 種類のマクロがある
◦ Freestanding macros ▪ 引数のみに依存して独立して展開される ▪ #function, #warning ◦ Attached macros ▪ 宣言に付属して対象の情報を利用して展開される ▪ @Observation © 2023 Wantedly, Inc. https://docs.swift.org/swift-book/documentation/the-swift- programming-language/macros/
Swift Macros • ソースコードのコンパイル時にコードを変換し、繰り返し書く手 間を省く • Swift では 2 種類のマクロがある
◦ Freestanding macros ▪ 引数のみに依存して独立して展開される ▪ #function, #warning ◦ Attached macros ▪ 宣言に付属して対象の情報を利用して展開される ▪ @Observation © 2023 Wantedly, Inc. https://docs.swift.org/swift-book/documentation/the-swift- programming-language/macros/
アプリの紹介 © 2023 Wantedly, Inc.
開発中のアプリの紹介 • Swift 5.9, iOS17 SDK の機能 を色々試してみる環境 • Apple
のドキュメントブラウザア プリ © 2023 Wantedly, Inc. https://github.com/swiftty/apple-documentation
public init © 2023 Wantedly, Inc.
public init • ドキュメントデータの構造の数だ け型を用意 • マルチモジュール構成で開発し ているため public init
が 全てに必要… ◦ Macro で楽をしたい © 2023 Wantedly, Inc.
@attached(member) • member ◦ 型に対してアタッチすることができる Macro ◦ 引数の declaration からプロパティなどのメンバー情報を取得
• →プロパティ定義を収集して init を生成 © 2023 Wantedly, Inc.
ImplicitInitMacro • members: MemberBlockItemListSyntax から 各プロパティの名前、型、初期値を取り出す © 2023 Wantedly, Inc.
ImplicitInitMacro • variables から引数リストを構築 ◦ init(FunctionParameterListSyntax) ◦ foo: Foo? =
nil を組み立て © 2023 Wantedly, Inc.
ImplicitInitMacro • init を構築 ◦ init() { for-loop } ◦
self.foo = foo を組み立て © 2023 Wantedly, Inc.
EnvironmentKey © 2023 Wantedly, Inc.
EnvironmentKey • SwiftUI の EnvironmentKey を定義する際に定型文が頻 発 © 2023 Wantedly,
Inc.
EnvironmentKey • SwiftUI の EnvironmentKey を定義する際に定型文が頻 発 © 2023 Wantedly,
Inc. このように書きたい
@attached(peer), @attached(accessor) • peer ◦ 任意の宣言と並べて新たな定義を用意できる Macro ▪ @AddAsync のようにメソッドを追加させたいときなど
• accessor ◦ プロパティの getter, setter を生成できる Macro © 2023 Wantedly, Inc.
SwiftUIEnvironmentMacro • peer マクロで EnvironmentKey に適合する型を生成 ◦ 返却する Syntax は
ExpressibleByStringInterpolation に 適合しているので文字列で実装することも可能 © 2023 Wantedly, Inc.
SwiftUIEnvironmentMacro • accessor マクロも同様に getter と setter の Syntax を
生成 ◦ peer で生成される型を参照 © 2023 Wantedly, Inc.
Tips © 2023 Wantedly, Inc.
Tips • Swift AST Explorer がやっぱり便利 ◦ https://swift-ast-explorer.com/ ◦ どこでどの
Syntax が現れるかを確認し て、キャストするため • SwiftSyntaxMacrosTestSupport の assertMacroExpansion も期待 する生成結果かどうか確認しやすい © 2023 Wantedly, Inc.
感想 • ボイラープレートで煩雑に感じていたコードがマクロでスッキ リするのは良い体験 • resultBuilder や ExpressibleByStringInterpolation のサポートで直感的に追加するコードを書ける •
dependency に swift-syntax が必要 ◦ 3rd party の Macro ライブラリだと微妙にニーズが満たせなかった り swift-syntax によって依存関係がシビアになりがちなので、しば らく自作が良さそう © 2023 Wantedly, Inc.