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 - Modern & Expressive, or Magical Unicorn?

731 views

Published on

Presentation on Swift 1.0 to give those coming from other languages a brief overview of the language. Presented first at Reasons to Be Creative, Brighton. 2015

Published in: Software
  • Be the first to comment

  • Be the first to like this

Swift - Modern & Expressive, or Magical Unicorn?

  1. 1. Modern & Expressive or Magical Unicorn? Mike Jones - @FlashGen Swift…
  2. 2. @flashgen | #reasonsto A point of reference, of sorts…
  3. 3. @flashgen | #reasonsto
  4. 4. @flashgen | #reasonsto var myNamevar = “Mike Jones”:String ;
  5. 5. @flashgen | #reasonsto var myName = “Mike Jones”
  6. 6. @flashgen | #reasonsto var myName:String = “Mike Jones”; var myName = “Mike Jones”
  7. 7. @flashgen | #reasonsto var myName = “Mike Jones” let myName = “Mike Jones”
  8. 8. @flashgen | #reasonsto Loops
  9. 9. Loops for var i = 0; i < 10; i++ { println(i) }
  10. 10. Loops for i in 0..<10 { println(i) }
  11. 11. Loops var myArray = [1,2,3,4,5] for item in myArray { println(item) }
  12. 12. Loops var i = 1 while i < 10 { println(i) i++ }
  13. 13. Loops var i = 10 do { println(i) i++ } while i < 10
  14. 14. @flashgen | #reasonsto Conditionals
  15. 15. If…else var num = 10 if num < 20 { println(“less than 20") } else { println("not less than 20") }
  16. 16. Switch…Case var value = 10 switch value { case 10: println("Yay! just right") case 20: println("a bit too high") default: println("Nothing to see here") }
  17. 17. Switch…Case var value = 6 switch value { case 1...9: println("Too low") case 10: println("Yay! just right") case 20: println("a bit too high") default: println("Nothing to see here") }
  18. 18. @flashgen | #reasonsto Optionals…
  19. 19. @flashgen | #reasonsto
  20. 20. Optionals var optionalInteger: Int? var optionalInteger: Optional<Int>
  21. 21. Optionals var impUnwrappedString:String! var impUnwrappedString:ImplicitlyUnwrappedOptional<String>
  22. 22. Optionals var myString = “hello” var myInt = myString.toInt()
  23. 23. Optionals var myString = “hello” var myInt = myString.toInt() // myInt = nil
  24. 24. Optionals var myString = “3” var myInt = myString.toInt()
  25. 25. Optionals var myString = “3” var myInt = myString.toInt() // myInt = 3
  26. 26. Optionals var myString = “3” var myInt:Int = myString.toInt()
  27. 27. Optionals var myString = “3” var myInt:Int = myString.toInt() // Object of type Optional is not unwrapped
  28. 28. Optionals var myString = “3” var myInt:Int = myString.toInt()!
  29. 29. Optionals var myString = “3” var myInt:Int = myString.toInt()! // myInt = 3
  30. 30. Optionals var myString = "hello" var myInt:Int = myString.toInt()!.advancedBy(5)
  31. 31. Optionals var myString = "hello" var myInt:Int = myString.toInt()!.advancedBy(5) // This fails
  32. 32. Optionals var myString = "5" var myOtherInt:Int = 0; if let myInt = myString.toInt()?.advancedBy(5) { myOtherInt = myInt; } else { println("It's nil") }
  33. 33. @flashgen | #reasonsto Functions & Closures
  34. 34. Functions func myFunc(valA:Int, valB:Int) -> Int { return valA + valB } var myInt = myFunc(4, 4);
  35. 35. Functions func myFunc(#valA:Int, #valB:Int) -> Int { return valA + valB } var myInt = myFunc(valA:4, valB:4);
  36. 36. Functions func myFunc(myAge valA:Int, yourAge valB:Int) -> Int { return valA + valB } var ourAgesCombined = myFunc(myAge:4, yourAge:4);
  37. 37. Tuples func sayHello(greeting:String, name:String) -> (String, String) { return (greeting + " " + name, “Pleased to meet you :)”) } var mygreeting = sayHello("Hello", "Reasons"); mygreeting.0; // Hello Reasons mygreeting.1; // Pleased to meet you :)
  38. 38. Tuples func sayHello(greeting:String, name:String) -> (fullGreeting:String, response:String) { return (greeting + " " + name, “Pleased to meet you :)”) } var mygreeting = sayHello("Hello", "Reasons"); mygreeting.fullGreeting; // Hello Reasons mygreeting.response; // Pleased to meet you :)
  39. 39. Tuples func sayHello(greeting:String, name:String) -> (String, String) { return (greeting + " " + name, "Pleased to meet you :)") } let (fullGreeting, response) = sayHello("Hello", "Reasons"); fullGreeting; // Hello Reasons response; // Pleased to meet you :)
  40. 40. Tuples func sayHello(greeting:String, name:String) -> (String, String) { return (greeting + " " + name, "Pleased to meet you :)") } let (fullGreeting, _) = sayHello("Hello", "Reasons"); fullGreeting; // Hello Reasons
  41. 41. Variadic Functions func addNumbers(numbers:Int...) -> Int { var total = 0; for num in numbers { total += num } return total; } addNumbers(1,2,3,4,5) // 15 addNumbers(1,2,3) // 6
  42. 42. Function Types func myFunc(valA:Int, valB:Int) -> Int { return valA + valB } var myIntFunc :(Int, Int) -> Int = myFunc myIntFunc(4,4)
  43. 43. Closures var namesArray = ["Mike", "Jason", "Dennis", "Darren", "John"]; func sortAlphabetically (s1:String, s2:String) -> Bool { return s1 < s2; } let myFriends = sorted(namesArray, sortAlphabetically)
  44. 44. Closures var namesArray = ["Mike", "Darren", "Dennis", "Jason", "Macca"]; let myFriends = sorted(namesArray, { (s1:String, s2:String) -> Bool in return s1 < s2; })
  45. 45. in…out var month = 9 var year = 2015 func swapTwoInts(inout a: Int, inout b: Int) { let tempA = a a = b b = tempA } swapTwoInts(&month, &year) // month = 2015, year = 9
  46. 46. @flashgen | #reasonsto Structs, Classes, Protocols & Extensions
  47. 47. @flashgen | #reasonsto Structs
  48. 48. Structs struct Person { var firstName: String = "Mike"; var lastName: String = "Jones"; } let myPerson = Person()
  49. 49. Classes class Person { var firstName: String; var lastName: String; init(fName:String, lName:String) { firstName = fName; lastName = lName; } } let myPerson = Person(fName:"Mike", lName:"Jones")
  50. 50. Classes class Person { var firstName: String; var lastName: String; init(fName:String="John", lName:String="Smith") { firstName = fName; lastName = lName; } } let myPerson = Person(fName:"Mike", lName:”Jones”) // fName = "Mike", lName = "Jones" let myDefaultPerson = Person() // fName = "John", lName = "Smith"
  51. 51. Classes class Person { var firstName: String; var lastName: String; init(fName:String, lName:String) { firstName = fName; lastName = lName; } convenience init() { self.init(fName: "John", lName: "Smith"); } } let myPerson = Person() // fName = "John", lName = "Smith"
  52. 52. Classes class Person { var firstName: String; var lastName: String; init(fName:String, lName:String) { firstName = fName; lastName = lName; } deinit { // Clean up here } }
  53. 53. Classes class Person { var firstName: String; var lastName: String; var fullName:String { get { return self.firstName + " " + self.lastName } } // … }
  54. 54. Inheritance class Employee: Person { var jobDescription :String = "I'm a developer"; func whatAreYouDoing() -> String { return "I'm working." } }
  55. 55. Inheritance public class Manager: Employee { override var jobDescription :String { get { return super.jobDescription } set(value) { super.jobDescription = value; } } override func whatAreYouDoing() -> String { return "Very little - I'm the boss!" } }
  56. 56. @flashgen | #reasonsto Protocols
  57. 57. Protocols protocol UnicornsProt { var unicornHerd:Int? { get set } func howManyUnicornsDoYouOwn() -> Int? }
  58. 58. Protocols class Person: UnicornsProt { var unicornHerd: Int?; func howManyUnicornsDoYouOwn() -> Int? { return unicornHerd; } }
  59. 59. @flashgen | #reasonsto Extensions
  60. 60. Extensions extension Person { public var unicorns:String { return "UNICORNS!" } public func moreUnicorns() -> String { return “MORE " + unicorns; } }
  61. 61. @flashgen | #reasonsto Generics…
  62. 62. Generics var myArray :Array<String> var myDict :Dictionary<String, Person> var myOptionalInt :Optional<Int>
  63. 63. Generics func swapTwoInts(inout a: Int, inout b: Int) { let tempA = a a = b b = tempA } func swapTwoStrings(inout a: String, inout b: String) { let tempA = a a = b b = tempA } func swapTwoDoubles(inout a: Double, inout b: Double) { let tempA = a a = b b = tempA }
  64. 64. Generics var month = 9 var year = 2015 var greeting = "Hello Reasons" var response = "Pleased to meet you :)" func swapTwoValues<T>(inout a: T, inout b: T) { let tempA = a a = b b = tempA } swapTwoValues(&month, &year) // month = 2015, year = 9 swapTwoValues(&greeting, &response) // greeting = "Pleased to meet you :) // response = "Hello Reasons
  65. 65. @flashgen | #reasonsto one more thing…
  66. 66. @flashgen | #reasonsto Swift 2.0
  67. 67. @flashgen | #reasonsto Open Source
  68. 68. @flashgen | #reasonsto Apple Developer Program http://developer.apple.com
  69. 69. @flashgen | #reasonsto Thank You
  70. 70. Mike Jones pixadecimal.com blog.flashgen.com @FlashGen

×