Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Less Conventional Swift (Lighting Talk)

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

Less Conventional Swift (Lighting Talk)

20 slides sharing nuggets of less conventional but very powerful Swift code. From Melbourne CocoaHeads #96 on the 14th of July 2016.

Avatar for Gio Lodi

Gio Lodi

July 14, 2016
Tweet

More Decks by Gio Lodi

Other Decks in Programming

Transcript

  1. Free Functions / / F i l e . s

    w i f t f u n c t r a n s f o r m ( i n p u t : A ) - > B { / / . . . }
  2. enum for view state e n u m V i

    e w S t a t e < T > { c a s e E m p t y c a s e L o a d i n g c a s e L o a d e d ( T ) c a s e E r r o r e d ( E r r o r P r o t o c o l ) }
  3. Either e n u m E i t h e

    r < A , B > { c a s e L e f t ( A ) c a s e R i g h t ( B ) }
  4. l e t d a t a : [ E

    i t h e r < P i z z a , A d > ] = [ / / . . . ] f u n c t a b l e V i e w ( _ t a b l e V i e w : U I T a b l e V i e w , c e l l F o r R o w A t i n d e x P a t h : I n d e x P a t h ) - > U I T a b l e V i e w C e l l { l e t i t e m = d a t a [ i n d e x P a t h . r o w ] l e t i d e n t i f i e r = i d e n t i f i e r ( f o r I t e m : i t e m ) l e t c e l l = t a b l e V i e w . d e q u e u e R e u s a b l e C e l l ( w i t h I d e n t i f i e r : i d e n t i f i e r , f o r : i n d e x P a t h ) s w i t c h i t e m { c a s e . L e f t ( l e t m o d e l ) : c o n f i g u r e ( c e l l , w i t h M o d e l : m o d e l ) c a s e . R i g h t ( l e t a d ) : c o n f i g u r e ( c e l l , w i t h A d : a d ) } r e t u r n c e l l }
  5. Result e n u m R e s u l

    t < T > { c a s e S u c c e s s ( T ) c a s e F a i l u r e ( E r r o r P r o t o c o l ) }
  6. l e t t a s k = s e

    s s i o n . d a t a T a s k ( w i t h : r e q u e s t ) { ( r e s u l t : R e s u l t < ( D a t a s w i t c h r e s u l t { c a s e . S u c c e s s ( l e t d a t a , l e t r e s p o n s e ) : / / D o s t u f f w i t h d a t a a n d r e s p o n s e b r e a k c a s e . F a i l u r e ( l e t e r r o r ) : / / D o s t u f f w i t h e r r o r b r e a k } } t a s k . r e s u m e ( )
  7. https:/ /github.com/antitypical/Result m a p , f l a t

    M a p m a p E r r o r , f l a t M a p E r r o r
  8. materialize d o { l e t j s o

    n = t r y J S O N S e r i a l i z a t i o n . j s o n O b j e c t ( w i t h : d a t a , o p t i o n s : [ ] ) } c a t c h { p r i n t ( e r r o r ) } / / / / m a t e r i a l i z e ! / / l e t j s o n = m a t e r i a l i z e { t r y J S O N S e r i a l i z a t i o n . j s o n O b j e c t ( w i t h : d a t a , o p t i o n s : [ ] ) } s w i t c h j s o n { c a s e . S u c c e s s ( l e t o b j e c t ) : / / D o s t u f f w i t h a c t u a l v a l u e b r e a k c a s e . F a i l u r e ( l e t e r r o r ) : / / D o s t u f f w i t h e r r o r b r e a k }
  9. O p t i o n a l < T

    > E i t h e r < A , B > R e s u l t < T >
  10. GenericTableViewController l e t v c : G e n

    e r i c T a b l e V i e w C o n t r o l l e r < S t r i n g > = t a b l e V i e w C o n t r o l l e r ( w i t h : p i z z a N a m e s , c e l l C l a s s : U I T a b l e V i e w C e l l . s e l f , c o n f i g u r a t o r : { v a l u e , c e l l i n c e l l . t e x t L a b e l ? . t e x t = v a l u e } ) https:/ /github.com/mokagio/GenericTableViewController
  11. Dependant Types N o t E m p t y

    A r r a y P h o n e N u m b e r B o u n d e d I n t