Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
ダックタイピングとidでUserDefaultsをモック化する
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
417.72KI
September 05, 2019
Programming
3k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ダックタイピングとidでUserDefaultsをモック化する
https://fortee.jp/iosdc-japan-2019/proposal/873b4cdb-4c92-4111-bf0b-67a67dbb242e
417.72KI
September 05, 2019
More Decks by 417.72KI
See All by 417.72KI
iOSDC 2026
417_72ki
0
45
Comparing decimals in Swift Testing
417_72ki
0
260
Reboot a personal app abandoned for 10 years with recent techs
417_72ki
0
130
iTunes・おぼえていますか〜ScriptingBridge今昔物語〜
417_72ki
1
250
The history of entry-point in iOS app Development
417_72ki
0
550
R.swift to Asset Symbols
417_72ki
0
410
Refactor with using `available` and `deprecated`
417_72ki
3
860
CLIツールにSwift Concurrencyを適用させようとしている話
417_72ki
3
530
CI with Danger-Swift
417_72ki
1
290
Other Decks in Programming
See All in Programming
Simple Storage Service(S3) is not simple
iwatsukayura
0
230
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
680
contenteditable と日本語入力に向き合う
colorful12
0
180
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
120
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
150
そのリトライ、死んだコネクションを使い回していませんか ── GoのHTTPクライアントとHTTP/2を実プロダクト障害から学び直す
myus4a
1
410
ジョジョの世界をXRで再現する
mofurune
0
140
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2k
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
140
AI時代のコードレビューは人に向けるな、仕組みに向けろ
texmeijin
5
3.5k
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
350
Featured
See All Featured
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
580
What the history of the web can teach us about the future of AI
inesmontani
PRO
2
730
4 Signs Your Business is Dying
shpigford
187
23k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
570
What's in a price? How to price your products and services
michaelherold
247
13k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
370
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Embracing the Ebb and Flow
colly
88
5.2k
Fireside Chat
paigeccino
43
4.1k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
450
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
Transcript
μοΫλΠϐϯάͱJEͰ 6TFS%FGBVMUTΛϞοΫԽ͢Δ J04%$+BQBOEBZ5SBDL&
struct Me { let name = "Takuhiro Muta" let aka
= "417.72KI" let twitter = "417_72ki" let qiita = "417_72ki" let gitHub = "417-72KI" let products = [ "BuildConfig.swift", "MockUserDefaults", ] let workAt = [ "Rhizome Inc.": "2014/04~2018/01", "iRidge Inc.": "2018/02~2019/08", "WealthNavi Inc.": "2019/09~", ] } TFMGEFTDSJQUJPO
"HFOEB w ςετͱ6TFS%FGBVMUT w μοΫλΠϐϯά w JEܕ w .PDL6TFS%FGBVMUT
ςετͱ6TFS%FGBVMUT
ςετͱ6TFS%FGBVMUT w 6OJU5FTUΛߟ͑Δ্Ͱ6TFS%FGBVMUTͷѻ͍ॏཁ w 6TFS%FGBVMUTΛ5FTUBCMFʹ͍ͨ͠ w QSPUPDPMΛט·ͤΔ w TVJUF/BNFΛ͏
QSPUPDPMΛט·ͤΔ protocol UserDefaultsProtocol: class { func object(forKey defaultName: String) ->
Any? func set(_ value: Any?, forKey defaultName: String) func string(forKey defaultName: String) -> String? ɾ ɾ ɾ } extension UserDefaults: UserDefaultsProtocol {}
QSPUPDPMΛט·ͤΔ class MockUserDefaults { private var mockDictionary: [String: Any] =
[:] } extension MockUserDefaults: UserDefaultsProtocol { func object(forKey defaultName: String) -> Any? { return mockDictionary[defaultName] } func set(_ value: Any?, forKey defaultName: String) { mockDictionary[defaultName] = value } ɾ ɾ ɾ }
QSPUPDPMΛט·ͤΔ protocol UseCase { var userDefaults: UserDefaultsProtocol { get }
func doSomething() } extension UseCase { func doSomething() { userDefaults.set("bar", forKey: "foo") } } class MixInUseCase: UseCase { let userDefaults: UserDefaultsProtocol = UserDefaults.standard } class TestUseCase: UseCase { let userDefaults: UserDefaultsProtocol = MockUserDefaults() } "QQ 5FTU
TVJUF/BNFΛ͏ protocol UseCase { var userDefaults: UserDefaults { get }
func doSomething() } extension UseCase { func doSomething() { userDefaults.set("bar", forKey: "foo") } } class MixInUseCase: UseCase { let userDefaults: UserDefaults = .standard } class TestUseCase: UseCase { let userDefaults: UserDefaults = .init(suiteName: "Mock")! } "QQ 5FTU
ςετͱ6TFS%FGBVMUT w 6TFS%FGBVMUTΛ5FTUBCMFʹ͍ͨ͠ w QSPUPDPMΛט·ͤΔ w طʹ6TFS%FGBVMUTΛϓϩύςΟͱͯ͠༻͍ͯ͠Δ ߹ɺطଘίʔυʹखΛೖΕΔඞཁ͕͋Δ w TVJUF/BNFΛ͏
w /4)PNF%JSFDUPSZ -JCSBSZ1SFGFSFODFT TVJUF/BNF QMJTU͕࡞͞ΕΔ w ςετͰӬଓྖҬΛ͍͡Γͨ͘ͳ͍
ςετͱ6TFS%FGBVMUT w 6TFS%FGBVMUTΛ5FTUBCMFʹ͍ͨ͠ w QSPUPDPMΛט·ͤΔ w TVJUF/BNFΛ͏ w μοΫλΠϐϯάΛͬͨࠇຐज़
μοΫλΠϐϯά
μοΫλΠϐϯά w ܕ͚ʹؔ͢Δߟ͑ํͷҰͭ w ಈతܕ͚ݴޠͰΘΕΔ FH3VCZ 1ZUIPO w ΦϒδΣΫτࣗମ͕ϝιουϓϩύςΟΛ͔࣋ͭͰఆ
OPUܕ r%BWF5IPNBT l*GJUXBMLTMJLFBEVDLBOERVBDLTMJLFBEVDL JUNVTUCFBEVDLz
Ұൠతͳ੩తܕ͚ ී௨ʹΫϥεࢦఆ ݘʹରͯ͠ Εʂ ϫϯʂ
Ұൠతͳ੩తܕ͚ JOUFSGBDFʹΑΔநԽ ಈʹରͯ͠ Εʂ ϫϯʂ
μοΫλΠϐϯάʹΑΔ ಈతܕ͚ Կ͔ΒΜ͕ͱΓ͋͑ͣ Εʂ Կ͔͔Βͳ͍͚Ͳ ΕͬͯݴΘΕͨΒ Δͣ
ϫϯʂ μοΫλΠϐϯάʹΑΔ ಈతܕ͚ Կ͔ΒΜ͕ͱΓ͋͑ͣ Εʂ
0CKFDUJWF$ʹ͓͚Δ μοΫλΠϐϯά @interface Duck: NSObject - (void)walk; - (void)quack; @end
@implementation Duck - (void)walk { NSLog(@"Duck walking"); } - (void)quack { NSLog(@"Quack!!"); } @end @interface Dog: NSObject - (void)walk; - (void)bark; @end @implementation Dog - (void)walk { NSLog(@"Dog walking"); } - (void)bark { NSLog(@"Bark!!"); } @end void walkAndQuack(id duck) { [duck walk]; [duck quack]; }
0CKFDUJWF$ʹ͓͚Δ μοΫλΠϐϯά walkAndQuack([Duck new]); // Duck walking // Quack!! walkAndQuack([Dog
new]); // Dog walking // unrecognized selector sent to instance
0CKFDUJWF$ʹ͓͚Δ μοΫλΠϐϯά void walkAndQuack(id duck) { [duck walk]; [duck quack];
} walkAndQuack([Duck new]); walkAndQuack([Dog new]);
0CKFDUJWF$ʹ͓͚Δ μοΫλΠϐϯά void walkAndQuack(Duck *duck) { [duck walk]; [duck quack];
} walkAndQuack([Duck new]); walkAndQuack([Dog new]); *ODPNQBUJCMFQPJOUFSUZQFTQBTTJOH%PH UPQBSBNFUFSPGUZQF%VDL ܯࠂ͕ग़Δ͚ͩͰϏϧυ௨Δ
0CKFDUJWF$ͷܕγεςϜ w ऑ͍੩తܕ͚ w ࣮ߦ࣌ʹܕͷνΣοΫΛ͠ͳ͍ w ܕ͕Ұக͠ͳ͍ΦϒδΣΫτΛೖͯ͠ ίϯύΠϧΤϥʔʹͳΒͳ͍ ܯࠂͷΈ
w ϝιουΛݺͿࡍʹ͕͋ΔͱΫϥογϡ͢Δ w +BWB4XJGUڧ͍੩తܕ͚
ڧ͍ܕ͚ͱऑ͍ܕ͚ 4USJOH < ߴࡔึ೫Ռ Ҁֆཬ > ऑ͍ܕ͚
ڧ͍ܕ͚
ऑ͍ܕ͚ w ࣮ߦ࣌ʹܕͷνΣοΫΛ͠ͳ͍ w ܕͷνΣοΫΛͤͣʹϝιουΛ࣮ߦ͢Δ w ֨ೲ͞Ε͍ͯΔΦϒδΣΫτ͕ͦͷϝιουΛ࣮ߦͰ ͖Ε0, %FMFHBUF
@protocol UITableViewDataSource<NSObject> @required - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; - (UITableViewCell *)tableView:(UITableView
*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; @optional - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; ɾ ɾ ɾ %FMFHBUFͱμοΫλΠϐϯά ࣮Λڧ੍͠ͳ͍ϝιου܈
@implementation UITableView ɾ ɾ ɾ - (NSInteger)numberOfSections { if ([self.dataSource
respondsToSelector:@selector(numberOfSectionsInTableView:)]) { return [self.dataSource numberOfSectionsInTableView:self]; } else { return 1; } } ɾ ɾ ɾ @end %FMFHBUFͱμοΫλΠϐϯά ϝιου͕࣮͞Ε͍ͯΔ͔νΣοΫ
%FMFHBUFͱμοΫλΠϐϯά @interface HogeViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @property (nonatomic, weak)
IBOutlet UITableView *tableView; @end @implementation HogeViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableView.dataSource = self; self.tableView.delegate = self; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { ɾɾɾ } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ɾɾɾ } @end ϓϩτίϧએݴ σϦήʔτϝιου
%FMFHBUFͱμοΫλΠϐϯά @interface HogeViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @property (nonatomic, weak)
IBOutlet UITableView *tableView; @end @implementation HogeViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableView.dataSource = self; self.tableView.delegate = self; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { ɾɾɾ } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ɾɾɾ } @end OVNCFS0G4FDUJPOT*O5BCMF7JFX͕࣮͞Ε͍ͯͳ͍ͷͰTFDUJPO
%FMFHBUFͱμοΫλΠϐϯά @interface HogeViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @property (nonatomic, weak)
IBOutlet UITableView *tableView; @end @implementation HogeViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableView.dataSource = self; self.tableView.delegate = self; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2 } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { ɾɾɾ } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ɾɾɾ } @end OVNCFS0G4FDUJPOT*O5BCMF7JFX͕࣮͞ΕͯΛฦ͢Α͏ʹ
@interface HogeViewController : UIViewController @property (nonatomic, weak) IBOutlet UITableView *tableView;
@end @implementation HogeViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableView.dataSource = self; self.tableView.delegate = self; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2 } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { ɾɾɾ } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ɾɾɾ } @end %FMFHBUFͱμοΫλΠϐϯά "TTJHOJOHUPJE6*5BCMF7JFX%BUB4PVSDF@/VMMBCMFGSPN JODPNQBUJCMFUZQF)PHF7JFX$POUSPMMFS DPOTU@@TUSPOH ϓϩτίϧએݴΛফͯ͠Έͨ߹ ܯࠂ͕ग़Δ͚ͩͰϏϧυ௨Δ Ϋϥογϡͤͣ5BCMF7JFXͱͯ͠ػೳ͢Δ
JEܕ
JEܕ w 0CKFDUJWF$ಠࣗͷ൚༻σʔλܕ w +BWBͰݴ͏0CKFDUɺ4XJGUͰݴ͏"OZ w ΦϒδΣΫτͷϙΠϯλΛอ࣋ w Ωϟετ͢Δ͜ͱͳ͘ೖϝιουݺͼग़͕͠Մೳ w
ܕ҆શੑΛࣺͯͯॊೈੑʹεςʔλεΛৼͬͯΔ
NSArray *array = @[ @{@"hoge":@"fuga"}, ]; for (NSObject *obj in
array) { NSLog([obj objectForKey:@"hoge"]); } JEܕͷԸܙ /PWJTJCMF!JOUFSGBDFGPS/40CKFDUEFDMBSFTUIFTFMFDUPSPCKFDU'PS,FZ
NSArray *array = @[ @{@"hoge":@"fuga"}, ]; for (NSObject *obj in
array) { NSLog([(NSDictionary *)obj objectForKey:@"hoge"]); } JEܕͷԸܙ Ωϟετ͢Δ͜ͱͰݺͼग़ͤΔΑ͏ʹ
NSArray *array = @[ @{@"hoge":@"fuga"}, ]; for (id obj in
array) { NSLog([obj objectForKey:@"hoge"]); } JEܕͷԸܙ Ωϟετͤͣʹ/4%JDUJPOBSZͱͯ͠ѻ͏͜ͱ͕Ͱ͖Δ
NSArray *array = @[ @{@"hoge":@"fuga"}, ]; for (NSDictionary *obj in
array) { NSLog([obj objectForKey:@"hoge"]); } JEܕͷԸܙ /4%JDUJPOBSZͱͯ͠औΓग़ͤΔ ॊೈੑͷߴ͍ίʔσΟϯά͕Մೳʹ
NSArray *array = @[ @{@"hoge":@"fuga"}, @"hogehoge" ]; for (NSDictionary *obj
in array) { NSLog([obj objectForKey:@"hoge"]); } JEܕͷԸܙ GVHB
NSArray *array = @[ @{@"hoge":@"fuga"}, @"hogehoge" ]; for (NSDictionary *obj
in array) { NSLog([obj objectForKey:@"hoge"]); } JEܕͷԸܙ VOSFDPHOJ[FETFMFDUPSTFOUUPJOTUBODF
NSArray <NSDictionary <NSString *, id> *> *array = @[ @{@"hoge":@"fuga"},
@"hogehoge" ]; for (NSDictionary <NSString *, id> *obj in array) { NSLog([obj objectForKey:@"hoge"]); } -JHIUXFJHIU(FOFSJDT 0CKFDUPGUZQF/44USJOH JTOPUDPNQBUJCMFXJUIBSSBZ FMFNFOUUZQF/4%JDUJPOBSZ/44USJOH JE
6TFS%FGBVMUTͱμοΫλΠϐϯά
w ऑ͍੩తܕ͚ w ࣮ߦ࣌ʹܕͷνΣοΫΛ͠ͳ͍ w ܕͷνΣοΫΛͤͣʹϝιουΛ࣮ߦ͢Δ w ֨ೲ͞Ε͍ͯΔΦϒδΣΫτ͕ͦͷϝιουΛ࣮ߦͰ͖Ε0, 6TFS%FGBVMUTͱμοΫλΠϐϯά /46TFS%FGBVMUT͕͍࣋ͬͯΔ
ϝιουΛͦͬ͘ΓਅࣅͨΫϥεΛ࡞ِͬͯͯ͋͛͠Δ
.PDL6TFS%FGBVMUT
.PDL6TFS%FGBVMUT IUUQTHJUIVCDPN,*.PDL6TFS%FGBVMUT
%FNP
ҙ w μοΫλΠϐϯάࣗମ͋͘·Ͱख๏ ߟ͑ํ Ͱ͋Γɺ ภʹࠇຐज़ͩͱݴ͍Εͳ͍ w ࣮ࡍɺ1ZUIPO1)1ͳͲͷಈతܕ͚ܥݴޠͰ͜ͷ ߟ͑ํʹج͍ͮͨϓϩάϥϛϯά͕ߦΘΕΔ w
ࢍ൱͋Δ͕ɺ͜ΕͰϙϦϞʔϑΟζϜΛ࣮ݱ͍ͯ͠Δ ͱ͍͏આ͋Δ
·ͱΊ w 0CKFDUJWF$ऑ͍ܕ͚ͳͷͰશؔ͘ແ͍ΫϥεͷΦ ϒδΣΫτΛೖͰ͖Δ w μοΫλΠϐϯάʹΑͬͯ/46TFS%FGBVMUTͱಉ͡ϝ ιουΛએݴͨ͠ผͷΫϥεΛએݴِ͕ͯ͠Մೳʹ w JEܕΛ͏͜ͱͰΩϟετෆཁ
5IBOLZPV IUUQTHJUIVCDPN,*.PDL6TFS%FGBVMUT
ࢀߟจݙ w μοΫɾλΠϐϯά 8JLJQFEJB IUUQTKBXJLJQFEJBPSHXJLJ &&&"'&##& #'&"&&#& #
w JEܕͱಈత݁߹ͷ֓೦ IUUQTKQSPHSBNFSDPNPCKFDUJWFD w ·ͭͱɹϓϩάϥϛϯάͷΦΩςୈճʢʣ IUUQTUFDIOJLLFJCQDPKQJUBSUJDMF$0-6./
ʲॏཁͳҙࣄ߲ʳ ɾຊࢿྉɺஅఆతஅΛఏڙ͢ΔͷͰͳ͘ɺใΛఏڙ͢Δ͜ͱͷΈΛతͱ͓ͯ͠Γɺ͍͔ͳΔछྨ ͷק༠͢ΔͷͰ͋Γ·ͤΜɻ࠷ऴతͳܾఆɺ͓٬༷ࣗͰஅ͢Δͷͱ͠ɺࣾ͜ΕʹҰ ؔ༩ͤͣɺ·ͨɺҰͷΛෛ͍·ͤΜɻ ɾຊࢿྉʹকདྷͷग़དྷࣄʹؔ͢Δ༧ؚ͕·Ε͍ͯΔ߹͕͋Γ·͕͢ɺͦΕΒ༧Ͱ͋Γɺ·ͨɺຊࢿ ྉͷ༰ͷਖ਼֬ੑɺ৴པੑɺશੑɺద࣌ੑΛҰอূ͢ΔͷͰ͋Γ·ͤΜɻຊࢿྉʹج͍ͮͯඃͬͨ ͍͔ͳΔଛʹ͍ͭͯɺࣾҰͷΛෛ͍·ͤΜɻ·ͨɺࣾɺ৽͍͠ใকདྷͷग़དྷࣄͦͷଞ ͷใʹ͍ͭͯɺߋ৽ຢగਖ਼͢ΔٛΛෛ͍·ͤΜɻ ɾຊࢿྉΛར༻͢Δ͜ͱʹΑΓ͓٬༷ʹੜͨ͡తଛɺؒతଛɺੜతଛͦͷଞ͍͔ͳΔଛʹͭ ͍ͯɺࣾҰͷΛෛ͍·ͤΜɻ
߸ɿΣϧεφϏגࣜձࣾۚ༥औҾۀऀؔ౦ࡒہʢۚʣୈ߸ Ճೖڠձɿຊূ݊ۀڠձҰൠࣾஂ๏ਓຊࢿސۀڠձ