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
通信処理を差し替えてみた話
Search
Tatsuya Tanaka
February 15, 2017
Technology
5
2.3k
通信処理を差し替えてみた話
URLProtocolとMethod Swizzlingの話
#potatotips 37
Tatsuya Tanaka
February 15, 2017
Tweet
Share
More Decks by Tatsuya Tanaka
See All by Tatsuya Tanaka
iPhoneのセンサー情報をmacOSアプリでリアルタイム活用するための技術
tattn
1
450
Better use of SwiftUI
tattn
0
350
Swift Concurrencyによる安全で快適な非同期処理
tattn
2
1k
iOSアプリの技術選択2022
tattn
7
3.7k
Widget Suggestions 対応と ヤフーの新OS対応
tattn
1
1.2k
WidgetKitで良い体験を作るには / Good experience with WidgetKit
tattn
2
1.4k
既存アプリにSwiftUIをどう組み込んでいくか
tattn
8
2.1k
iOS 14からのアプリ内課金
tattn
5
2.5k
iOS 14の位置情報系アップデート
tattn
0
22k
Other Decks in Technology
See All in Technology
VPoEの視点から見た、ヘンリーがサーバーサイドKotlinを使う理由 / Why Server-side Kotlin 2024
cho0o0
1
420
ペパボのオブザーバビリティ研修2024 説明資料
kesompochy
0
1.1k
Matterport を使ってクラスメソッド各拠点のバーチャルオフィスツアーを作成してみた
wakatsuki
0
160
Flutter研修【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
160
セキュリティ研修 Day1【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
160
E2Eテスト自動化プラットフォームにおけるAIの活用
shift_evolve
0
190
JBUG岡山 #6 WordCamp男木島の チームビルディング
takeshifurusato
0
150
データベース研修 分析向けSQL入門【MIXI 24新卒技術研修】
mixi_engineers
PRO
0
110
地理情報とAPIのトレンド
nagix
0
160
シフトレフトで挑む セキュリティの生産性向上
sekido
PRO
0
270
DDDにおける認可の扱いとKotlinにおける実装パターン / authorization-for-ddd-and-kotlin-implement-pattern
urmot
4
390
ACRiルーム最新情報とAMD GPUサーバーのご紹介
anjn
0
160
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
239
11k
Build The Right Thing And Hit Your Dates
maggiecrowley
28
2.2k
Why Our Code Smells
bkeepers
PRO
332
56k
Git: the NoSQL Database
bkeepers
PRO
423
64k
A designer walks into a library…
pauljervisheath
201
24k
The Invisible Customer
myddelton
117
13k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
78
15k
Ruby is Unlike a Banana
tanoku
96
10k
Why You Should Never Use an ORM
jnunemaker
PRO
51
8.9k
The Illustrated Children's Guide to Kubernetes
chrisshort
39
47k
We Have a Design System, Now What?
morganepeng
46
7k
How GitHub (no longer) Works
holman
305
140k
Transcript
௨৴ॲཧΛࠩ͠ସ͑ͯΈͨ
ࣗݾհ • ాதୡ (@tattn) • Ϡϑʔͷ2016৽ଔ
Qiitaͱ͔Twitterͱ͔ͬͯ·͢ • ։ൃ͕രʹͳΔSwift༻XcodeϓϥάΠϯΛ·ͱΊͯΈͨ • ͏ͱख์ͤͳ͘ͳΔSwift Extensionू (Swift3൛) @tanakasan2525
APIͷϢχοτςετͲ͏ͯ͠·͢ ͔ʁ
DIʁ ϥΠϒϥϦʁ
MockingjayศརͰ͢ΑͶ
Mockingjay https://github.com/kylef/Mockingjay let body = [ "user": "Kyle" ] stub(uri("/{user}/{repository}"),
json(body))
Ͳ͏͍͏ΈͰಈ͍ͯΔΜͩΖ͏ !
ௐͯΈΔͱ
URLProtocolͱMethod Swizzling Λͬͯ௨৴ॲཧΛࠩ͠ସ͍͑ͯ·͠ ͨ
URLProtocol https://developer.apple.com/reference/foundation/urlprotocol αϙʔτ֎ͷϓϩτίϧͰ௨৴͢Δ߹ ϦΫΤετΛಠࣗͷํ๏Ͱॲཧ͢Δ࣌ʹ͏Ϋϥεɻ
Mock༻ͷURLProtocol public class MockURLProtocol: URLProtocol { override open func startLoading()
{ let jsonString = "{\"mock\": \"data\"}" let json = jsonString.data(using: .utf8)! self.client?.urlProtocol(self, didLoad: json) self.client?.urlProtocolDidFinishLoading(self) } // ͦͷଞϝιουলུ }
Method SwizzlingͰϞοΫ༻ͷURLProtocol ʹࠩ͠ସ͑ public extension URLSessionConfiguration { public class func
setup() { let `default` = class_getClassMethod(URLSessionConfiguration.self, #selector(getter: URLSessionConfiguration.default)) let swizzled = class_getClassMethod(URLSessionConfiguration.self, #selector(getter: URLSessionConfiguration.mock)) method_exchangeImplementations(`default`, swizzled) } private dynamic class var mock: URLSessionConfiguration { let configuration = self.mock configuration.protocolClasses?.insert(MockURLProtocol.self, at: 0) URLProtocol.registerClass(MockURLProtocol.self) return configuration } }
ֶ͔ͤͬ͘ΜͩͷͰϥΠϒϥϦԽ͠· ͨ͠ !
Replacer https://github.com/tattn/Replacer ௨৴ͷελϒԽMethod SwizzlingΛαϙʔτ͢ΔϥΠϒϥϦ
Method ChainͰΦϓγϣϯΛઃఆ͠·͢ self.urlStub .url("echo.jsontest.com/[a-z]+/.*") .httpMethod(.post) .json(["test": "data"]) .delay(1.5)
QuickͱAlamofireͰͷελϒԽ describe("Quick compatibility test") { it("returns mock data") { let
url = "http://echo.jsontest.com/key/value/one/two" var json: [String: String]? Alamofire.request(url, method: .post).responseJSON { response in json = response.result.value as? [String: String] } expect(json?["test"]).toEventually(equal("data")) } }
ओͳػೳ • ௨৴ͷελϒԽ • XCTestҎ֎ (Host ApplicationؚΉ) Ͱ༻Մೳ • Method
Swizzlingͷαϙʔτػೳ ͳͲͰ͢ɻ
ྑ͔ͬͨΒͬͯΈ͍ͯͩ͘͞ ! (ʁʁ)ʻ ܅⭐͕େ͖ͳϑϨϯζͳΜͩͶʂ
͋ɺͦ͏͍͑ɺɺɺ
࡞͍ͬͯΔ࣌ʹSwiftͷόάʹؾ͕͖·ͨ͠ ڥ: Xcode 8.2, Swift 3.0.2
άϩʔόϧఆٛ͞ΕͨؔʹϞδϡʔϧ໊Λ͚ Δͱɺ@discardableResult͕ਖ਼ৗʹಈ࡞͠ͳ͍
ઌఔௐͯΈΔͱɺमਖ਼͞ΕͨΈ͍ͨͰ͢ ! https://bugs.swift.org/browse/SR-3665 Fix @discardableResult when invoking function with explicit
namespace #7201
SwiftͷਐԽɾվળࢭ ·Βͳ͍Ͱ͢Ͷʂ !
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝ ·ͨ͠ʂ