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
swiftty
October 17, 2023
Technology
0
660
個人開発アプリでの 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
61
iOS 18 から追加された SwiftUI の傾向について調べてみる
swiftty
3
850
Flutter アプリのリリースフローを考える
swiftty
0
350
Meet BrowserEngineKit
swiftty
0
1.9k
Cloud-managed certificates へ移行した話
swiftty
0
640
同じアプリを Flutter と SwiftUI で書いてみる
swiftty
0
600
Swift Package Manager へ移行した話
swiftty
0
720
SwiftLint にコントリビュートする
swiftty
1
4.6k
SwiftUI でリスト要素のインプレッションを計測する
swiftty
0
600
Other Decks in Technology
See All in Technology
AWS re:Invent 2025事前勉強会資料 / AWS re:Invent 2025 pre study meetup
kinunori
0
770
可観測性は開発環境から、開発環境にもオブザーバビリティ導入のススメ
layerx
PRO
4
1.8k
DMMの検索システムをSolrからElasticCloudに移行した話
hmaa_ryo
0
180
AIプロダクトのプロンプト実践テクニック / Practical Techniques for AI Product Prompts
saka2jp
0
120
マルチエージェントのチームビルディング_2025-10-25
shinoyamada
0
210
猫でもわかるAmazon Q Developer CLI 解体新書
kentapapa
1
130
20251027_findyさん_音声エージェントLT
almondo_event
2
490
dbtとAIエージェントを組み合わせて見えたデータ調査の新しい形
10xinc
6
1.4k
CNCFの視点で捉えるPlatform Engineering - 最新動向と展望 / Platform Engineering from the CNCF Perspective
hhiroshell
0
140
From Natural Language to K8s Operations: The MCP Architecture and Practice of kubectl-ai
appleboy
0
330
GraphRAG グラフDBを使ったLLM生成(自作漫画DBを用いた具体例を用いて)
seaturt1e
1
160
オブザーバビリティと育てた ID管理・認証認可基盤の歩み / The Journey of an ID Management, Authentication, and Authorization Platform Nurtured with Observability
kaminashi
1
1.2k
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
514
110k
Automating Front-end Workflow
addyosmani
1371
200k
A Tale of Four Properties
chriscoyier
161
23k
BBQ
matthewcrist
89
9.9k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.7k
How to train your dragon (web standard)
notwaldorf
97
6.3k
Designing Experiences People Love
moore
142
24k
The Pragmatic Product Professional
lauravandoore
36
7k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.7k
Raft: Consensus for Rubyists
vanstee
140
7.2k
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.