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
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
43
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
850
CLIツールにSwift Concurrencyを適用させようとしている話
417_72ki
3
530
CI with Danger-Swift
417_72ki
1
290
Other Decks in Programming
See All in Programming
Agents on Rails - Rails at Scale 2026
irinanazarova
0
330
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
110
iOSDCのペンライトを自動制御したい!
akkeylab
0
360
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
120
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
230
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
130
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
8
5.5k
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
720
AI時代のコードレビューは人に向けるな、仕組みに向けろ
texmeijin
5
3.4k
速習iPhone Duo対応
yuukiw00w
2
960
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
510
wkhtmltopdfの次どうするか問題2026
willnet
2
1.8k
Featured
See All Featured
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
550
Faster Mobile Websites
deanohume
310
32k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1k
Test your architecture with Archunit
thirion
2
2.4k
Unsuck your backbone
ammeep
672
58k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
390
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
For a Future-Friendly Web
brad_frost
183
10k
Skip the Path - Find Your Career Trail
mkilby
1
240
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./
ʲॏཁͳҙࣄ߲ʳ ɾຊࢿྉɺஅఆతஅΛఏڙ͢ΔͷͰͳ͘ɺใΛఏڙ͢Δ͜ͱͷΈΛతͱ͓ͯ͠Γɺ͍͔ͳΔछྨ ͷק༠͢ΔͷͰ͋Γ·ͤΜɻ࠷ऴతͳܾఆɺ͓٬༷ࣗͰஅ͢Δͷͱ͠ɺࣾ͜ΕʹҰ ؔ༩ͤͣɺ·ͨɺҰͷΛෛ͍·ͤΜɻ ɾຊࢿྉʹকདྷͷग़དྷࣄʹؔ͢Δ༧ؚ͕·Ε͍ͯΔ߹͕͋Γ·͕͢ɺͦΕΒ༧Ͱ͋Γɺ·ͨɺຊࢿ ྉͷ༰ͷਖ਼֬ੑɺ৴པੑɺશੑɺద࣌ੑΛҰอূ͢ΔͷͰ͋Γ·ͤΜɻຊࢿྉʹج͍ͮͯඃͬͨ ͍͔ͳΔଛʹ͍ͭͯɺࣾҰͷΛෛ͍·ͤΜɻ·ͨɺࣾɺ৽͍͠ใকདྷͷग़དྷࣄͦͷଞ ͷใʹ͍ͭͯɺߋ৽ຢగਖ਼͢ΔٛΛෛ͍·ͤΜɻ ɾຊࢿྉΛར༻͢Δ͜ͱʹΑΓ͓٬༷ʹੜͨ͡తଛɺؒతଛɺੜతଛͦͷଞ͍͔ͳΔଛʹͭ ͍ͯɺࣾҰͷΛෛ͍·ͤΜɻ
߸ɿΣϧεφϏגࣜձࣾۚ༥औҾۀऀؔ౦ࡒہʢۚʣୈ߸ Ճೖڠձɿຊূ݊ۀڠձҰൠࣾஂ๏ਓຊࢿސۀڠձ