THE OBJECTIVE-C WAY. â¢Observable post notifications in. NSNotificationCenter. â¢Observer subscribe to notifications b
Créateur d’applications mobiles
#TECHTALK
TechTalk gratuits et ouverts au public au sein de nos locaux ! N’hésitez pas à vous inscrire aux prochaines sessions sur notre site: ripplemotion-services.fr
#TECHTALK
TechTalk sous forme de courtes formations le mercredi de 17h à 18h ! Échange entre vous et nos spécialistes en développement mobile
TECHTALK ANIMÉ PAR :
OLIVIER TABONE Co-fondateur / Directeur
[email protected] 06 83 01 05 76
OBSERVER PATTERN SWIFT 3 Goodbye Notification Center
RMS TechTalk#1 - 12.10.2016
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
OBSERVER PATTERN SWIFT 3
DECOUPLING • 1 Observable - N observers
• The observable does not know anything about observers
Goodbye Notification Center
6
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
OBSERVER PATTERN SWIFT 3
Goodbye Notification Center
THE OBJECTIVE-C WAY •Observable post notifications in NSNotificationCenter •Observer subscribe to notifications by name and sender •Payload un notification’s userInfo
7
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
OBSERVER PATTERN SWIFT 3
Goodbye Notification Center
THE SWIFT3 WAY •Observable post notifications in NSNotificationCenter •Observer subscribe to notifications by name and sender •Payload un notification’s userInfo
8
THE SWIFT3 WAY
T
Goodbye Notification Center
•Observer subscribe to notifications by name and sender •Payload un notification’s userInfo
O
OBSERVER PATTERN SWIFT 3
•Observable post notifications in NSNotificationCenter
N
OLIVIER TABONE Co-fondateur / Directeur
SW IF TY
TechTalk #1 - 12.10.2016
9
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
OBSERVER PATTERN SWIFT 3
Goodbye Notification Center
NSNOTIFICATION DOES NOT PLAY WELL WITH SWIFT •No type checking at compile time •No null checking at compile time •Can’t wrap pure swift data structures (struct, enums, … everything that is not marked @objc •+ crash if observer is dealloc’ed before removal from the observable 10
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
LET’S BUILD AN OBSERVER !
Observable.playground 001 STRUCTURE
11
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
LET’S BUILD AN OBSERVER !
Observable.playground 002 NAIVE
12
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
LET’S MAKE OBSERVER AN EQUATABLE
stackoverflow.com/ questions/24926310/ what-does-protocol-canonly-be-used-as-a-genericconstraint-because-it-has
13
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
PROTOCOL ORIENTED PROGRAMMING
14
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
•Protocol ! = Protocol with associated types •Self is an implicit associated type
OBSERVER PATTERN SWIFT 3
Goodbye Notification Center
•Equatable uses Self
15
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
OK LET’S COMPARE MEMORY ADDRESSES
Observer protocol can be applied to a struct !
16
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
OBSERVERS HAVE REFERENCE SEMANTICS
17
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
OBSERVER PATTERN SWIFT 3
Goodbye Notification Center
CODE COMPILES ! 005 Naive AnyObject
TechTalk #1 - 12.10.2016
WHAT’S WRONG WITH THIS ?
OLIVIER TABONE Co-fondateur / Directeur
OBSERVER PATTERN SWIFT 3
Goodbye Notification Center
19
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
STORE WEAK REFERENCES
Swift does not find a static dispatch, should we force a dynamic dispatch ?
20
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
@OBJC PROTOCOLS •@objc is magic at first (restores old dynamic dispatch behaviour) •but can’t pass params with non-objc equivalent
OBSERVER PATTERN SWIFT 3
Goodbye Notification Center
21
TechTalk #1 - 12.10.2016
WEAK CONTAINERS
OLIVIER TABONE Co-fondateur / Directeur
OBSERVER PATTERN SWIFT 3
Goodbye Notification Center
NSHashTable.weakObjects() @objc again
22
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
WEAKSET
Make the compiler happy !
23
TechTalk #1 - 12.10.2016
OLIVIER TABONE Co-fondateur / Directeur
AND HERE WE ARE !
24
Merci de votre participation !
OLIVIER TABONE Co-fondateur / Directeur
[email protected] 06 83 01 05 76
Créateur d’applications mobiles