Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Swift Thinking

7,499 views

Published on

Swift Thinking. First presented at UIKonf http://www.uikonf.com/

Published in: Software, Technology

Swift Thinking

  1. Swift Thinking @NatashaTheRobot
  2. Back to December #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); } return 0; }
  3. println("Hello, World!")
  4. —Optionals?! —Value Types —Higher-order functions
  5. Optionals?!
  6. "~40% of bugs shipped to customers in the last three years would have been caught immediately by using Swift" - Sunset Lake Software
  7. enum Optional<T> { case Some(T) case None }
  8. let tSwiftAlbums = [ 2014 : "1989", 2012: "Red", 2010: "Speak Now", 2008: "Fearless", 2006: "Taylor Swift"] let possibleAlbumFrom2011: String? = tSwiftAlbums[2011] let possibleAlbumFrom2014: String? = tSwiftAlbums[2014]
  9. let tSwiftAlbums = [ 2014 : "1989", 2012: "Red", 2010: "Speak Now", 2008: "Fearless", 2006: "Taylor Swift"] let possibleAlbumFrom2014: String? = tSwiftAlbums[2014] if possibleAlbumFrom2014 == .None { println("Taylor Swift had no albums in 2014") } else { let albumFrom2014 = possibleAlbumFrom2014! println("Taylor Swift's 2014 album was (albumFrom2014)") }
  10. let tSwiftAlbums = [ 2014 : "1989", 2012: "Red", 2010: "Speak Now", 2008: "Fearless", 2006: "Taylor Swift"] if let albumFor2014 = tSwiftAlbums[2014] { println("Taylor Swift's 2014 album was (albumFor2014)") } else { println("Taylor Swift had no albums in 2014") }
  11. Value Types
  12. —structs —enums —(tuples)
  13. class Album { let title: String let artist: String var copiesSold: Int init(title: String, artist: String, copiesSold: Int) { self.title = title self.artist = artist self.copiesSold = copiesSold } }
  14. let tSwift1989 = Album(title: "1989", artist: "Taylor Swift", copiesSold: 4505000) func another1000Sales(forAlbum album: Album) { album.copiesSold += 1000 } another1000Sales(forAlbum: tSwift1989) tSwift1989.copiesSold // 4,506,000
  15. struct Album { let title: String let artist: String var copiesSold: Int }
  16. let tSwift1989 = Album(title: "1989", artist: "Taylor Swift", copiesSold: 4505000) func another1000Sales(var forAlbum album: Album) { album.copiesSold += 1000 album.copiesSold // 4,506,000 } another1000Sales(forAlbum: tSwift1989) tSwift1989.copiesSold // 4,505,000
  17. Use a value type when: —Comparing instance data with == makes sense —You want copies to have independent state —The data will be used in code across multiple threads Swift Blog: Value and Reference Types
  18. Use a reference type (e.g. use a class) when: —Comparing instance identity with == makes sense —You want to create shared, mutable state Swift Blog: Value and Reference Types
  19. "Almost all types in Swift are value types, including arrays, dictionaries, numbers, booleans, tuples, and enums. Classes are the exception rather than the rule." - Functional Swift Book
  20. $ grep -e "^struct " swift.md | wc -l 81 $ grep -e "^enum " swift.md | wc -l 8 $ grep -e "^class " swift.md | wc -l 3
  21. Higher-order Functions
  22. a higher-order function is a function that does at least one of the following: —takes one or more functions as an input —outputs a function - Wikipedia
  23. Array —map —reduce —filter —flatMap
  24. struct Song { let title: String let album: String } let tSwiftSongs = [ Song(title: "Blank Space", album: "1989"), Song(title: "All You Had to Do Was Stay", album: "Red"), Song(title: "Back to December", album: "Speak Now"), Song(title: "All You Had to Do Was Stay", album: "1989"), Song(title: "Begin Again", album: "Red"), Song(title: "Clean", album: "1989"), Song(title: "Love Story", album: "Fearless"), Song(title: "Shake It Off", album: "1989"), Song(title: "Bad Blood", album: "1989") ]
  25. struct tSwift1989Album { let title = "1989" var songs = [Song]() }
  26. class tSwift1989Album { let title = "1989" var songs = [Song]() func add1989Songs() { for song in tSwiftSongs { if song.album == "1989" { songs.append(song) } } } }
  27. let album = tSwift1989Album() album.add1989Songs() album.songs.count // 5
  28. let album = tSwift1989Album() album.add1989Songs() album.songs.count // 5 // MUCH FURTHER DOWN album.add1989Songs()
  29. let album = tSwift1989Album() album.add1989Songs() album.songs.count // 5 // MUCH FURTHER DOWN album.add1989Songs() album.songs.count // 10
  30. let album = tSwift1989Album() album.add1989Songs() album.songs.count // 5 album.add1989Songs() album.songs.count // 10 album.add1989Songs() album.songs.count // 15 album.add1989Songs() album.songs.count // 20
  31. /// Return an `Array` containing the elements `x` of `self` for which /// `includeElement(x)` is `true` func filter(includeElement: (T) -> Bool) -> [T]
  32. class tSwift1989Album { let title = "1989" var songs = [Song]() func add1989SongsWithFilter() { songs = tSwiftSongs.filter({ song in song.album == "1989"}) } }
  33. songs = tSwiftSongs.filter({ song in song.album == "1989"})
  34. songs = tSwiftSongs.filter({ song in song.album == "1989"}) songs = tSwiftSongs.filter({ $0.album == "1989"})
  35. songs = tSwiftSongs.filter({ song in song.album == "1989"}) songs = tSwiftSongs.filter({ $0.album == "1989"}) songs = tSwiftSongs.filter { $0.album == "1989"}
  36. let album = tSwift1989Album() album.add1989SongsWithFilter() album.songs.count // 5 album.add1989SongsWithFilter() album.songs.count // 5 album.add1989SongsWithFilter() album.songs.count // 5 album.add1989SongsWithFilter() album.songs.count // 5 album.add1989SongsWithFilter() album.songs.count // 5
  37. Swift Thinking —Optionals?! —Value Types —Higher-order functions
  38. Questions?! @NatashaTheRobot

×