What makes Swift
awesome
DMI Internal Tech Talk
Sokna Ly
Associate Software Engineer
Overview
• Announced on June 2, 2014 - Apple
• Open-source on Dec 3, 2015
• Version 2.1 (Swift 2.2 will release this Spring and
Swift 3.0 will come this fall)
• Xcode 6+
• iOS 7+, OS X 10.9 +, watchOS, tvOS, Linux
Content
• Protocol-Oriented Programming
• Literal Convertible
• Generic
• Operators
• Others
Protocol-Oriented
Programming (POP)
• Blueprint or interface that defines a set of
properties and methods (no implementation)
• Any type conformed to protocol must provides
its implementation
• Around 89 public protocols in Swift Standard
Library
Protocol-Oriented
Programming (POP) (Conti.)
•Basic
protocol Flyble {
func fly()
}
struct Plane: Flyble {
func fly() {
print("YahoooooOO! I'm on the sky.")
}
}
struct JetPack: Flyble {
func fly() {
print("YahoooooOO! I'm on the sky.")
}
}
let boeing = Plane()
boeing.fly()
Protocol-Oriented
Programming (POP) (Conti.)
•Extension (Swift 2.0)
protocol Flyble {
func fly()
}
extension Flyble {
func fly() {
print("YahoooooOO! I'm on the sky.")
}
}
struct Plane: Flyble {
var seatCount: Int
}
struct JetPack: Flyble { }
let boeing = Plane()
boeing.fly()
Protocol-Oriented
Programming (POP) (Conti.)
•Extension with condition clause
extension CollectionType where Self.Generator.Element == String {
func addPrefix(prefix prefix: String) -> [String] {
return self.map { prefix + $0 }
}
}
let classNames = ["HomeViewController","ConfirmView","MarqueeLabel"]
print(classNames.addPrefix(prefix: “DMI"))
//result
["DMIHomeViewController","DMIConfirmView","DMIMarqueeLabel"]
Protocol-Oriented
Programming (POP) (Conti.)
Protocol or Inheritance?
Literal Convertible
//String
let firstSelector: Selector = "doSomethingSelector"
let secondSelector: Selector = Selector("doSomethingElse")
//Array
let peoples: [Person] = [Person(name: "Sokna"), Person(name:
"Swift")]
let anotherPeoples: [Person] = [["name":"MyEx-Objc"],
["name":"MyGf-Swift"]]
• Selector struct can be initialized both from Selector
constructor itself and String?
• Array of Person can be initialized from array from Person
and array of dictionary?
Literal Convertible (Conti.)
struct Person {
var name: String!
}
extension Person: DictionaryLiteralConvertible {
typealias Key = String
typealias Value = String
init(dictionaryLiteral elements: (Key, Value)...) {
for (key, value) in elements where key ==
"name" {
self.name = value
}
}
}
Literal Convertible (Conti.)
extension Selector: StringLiteralConvertible {
typealias StringLiteralType = String
init(extendedGraphemeClusterLiteral value: StringLiteralType) {
self.value = value
}
init(unicodeScalarLiteral value: StringLiteralType) {
self.value = value
}
init(stringLiteral value: StringLiteralType) {
self.value = value
}
}
Literal Convertible (Conti.)
• ArrayLiteralConvertible
• BooleanLiteralConvertible
• DictionaryLiteralConvertible
• ExtendedGraphemeClusterLiteralConvertible
• FloatLiteralConvertible
• IntegerLiteralConvertible
• StringLiteralConvertible
• StringInterpolationConvertible
• UnicodeScalarLiteralConvertible
Generic
func randomInt(integers: [Int]) -> Int {
let randomIndex = Int(arc4random_uniform(UInt32(integers.count)))
return integers[randomIndex]
}
func randomFloat(floats: [Float]) -> Float {
let randomIndex = Int(arc4random_uniform(UInt32(floats.count)))
return floats[randomIndex]
}
func randomString(strings: [String]) -> String {
let randomIndex = Int(arc4random_uniform(UInt32(strings.count)))
return strings[randomIndex]
}
func randomElement<T>(elements: [T]) -> T {
let randomIndex = Int(arc4random_uniform(UInt32(elements.count)))
return elements[randomIndex]
}
Generic
struct RandomContainer<T> {
let items: [T]
init(items: [T]) {
self.items = items
}
func getRandom() -> T {
let randomIndex = Int(arc4random_uniform(UInt32(items.count)))
return items[randomIndex]
}
}
let integers = [1,2,3,4,5]
let randomer = RandomContainer(items: integers)
randomer.getRandom()
let names = ["A","B","C"]
let namesRandomer = RandomContainer(items: names)
nameRandomer.getRandom()
Operators
Operator Description Associativi
ty
Precedence
++ Increment (prefix)
- - Decrement (postfix)
+ Add (infix) Left 140
- Substract (infix) Left 140
* Multiply (infix) Left 150
/ Divide (infix) Left 150
?? Nil Coalescing Right 131
• More than 50 default operators
• Three types: prefix, infix, postfix
Operators (Conti.)
let dict = ["name":"Sokna"]
let myName = dict["name"] ?? "UnkownName"
• nil-coalescing operator (??) : return unwrapped
value or an alternative value in the case that the
optional value is nil
infix operator ?? { associativity right precedence 131 }
func ??<T>(lhs: T?, rhs: T) -> T {
guard let lhs = lhs else { return rhs}
return lhs
}
Operators (Conti.)
let optionalString: String? = "optional"
• ?: Optional operator: Indicate the possibility that
a value might be absent (nil) or not
postfix operator ? {}
postfix func ?<T>(lhs: T) {
Optional.Some(lhs)
}
Others
• Value-Oriented Programming
• Pattern Matching
• Dynamic Dispatch & Whole module optimization
• enum
• Bridgeable with Objective-C code
Others (Conti.)
• Sever-side Swift
• http://perfect.org
• https://github.com/NSNotFound/blog.swift
• https://github.com/Zewo
• https://github.com/Zewo/Epoch
• https://github.com/Zewo/OpenSSL
Thanks 🐦

What make Swift Awesome

  • 1.
    What makes Swift awesome DMIInternal Tech Talk Sokna Ly Associate Software Engineer
  • 2.
    Overview • Announced onJune 2, 2014 - Apple • Open-source on Dec 3, 2015 • Version 2.1 (Swift 2.2 will release this Spring and Swift 3.0 will come this fall) • Xcode 6+ • iOS 7+, OS X 10.9 +, watchOS, tvOS, Linux
  • 3.
    Content • Protocol-Oriented Programming •Literal Convertible • Generic • Operators • Others
  • 4.
    Protocol-Oriented Programming (POP) • Blueprintor interface that defines a set of properties and methods (no implementation) • Any type conformed to protocol must provides its implementation • Around 89 public protocols in Swift Standard Library
  • 5.
    Protocol-Oriented Programming (POP) (Conti.) •Basic protocolFlyble { func fly() } struct Plane: Flyble { func fly() { print("YahoooooOO! I'm on the sky.") } } struct JetPack: Flyble { func fly() { print("YahoooooOO! I'm on the sky.") } } let boeing = Plane() boeing.fly()
  • 6.
    Protocol-Oriented Programming (POP) (Conti.) •Extension(Swift 2.0) protocol Flyble { func fly() } extension Flyble { func fly() { print("YahoooooOO! I'm on the sky.") } } struct Plane: Flyble { var seatCount: Int } struct JetPack: Flyble { } let boeing = Plane() boeing.fly()
  • 7.
    Protocol-Oriented Programming (POP) (Conti.) •Extensionwith condition clause extension CollectionType where Self.Generator.Element == String { func addPrefix(prefix prefix: String) -> [String] { return self.map { prefix + $0 } } } let classNames = ["HomeViewController","ConfirmView","MarqueeLabel"] print(classNames.addPrefix(prefix: “DMI")) //result ["DMIHomeViewController","DMIConfirmView","DMIMarqueeLabel"]
  • 8.
  • 9.
    Literal Convertible //String let firstSelector:Selector = "doSomethingSelector" let secondSelector: Selector = Selector("doSomethingElse") //Array let peoples: [Person] = [Person(name: "Sokna"), Person(name: "Swift")] let anotherPeoples: [Person] = [["name":"MyEx-Objc"], ["name":"MyGf-Swift"]] • Selector struct can be initialized both from Selector constructor itself and String? • Array of Person can be initialized from array from Person and array of dictionary?
  • 10.
    Literal Convertible (Conti.) structPerson { var name: String! } extension Person: DictionaryLiteralConvertible { typealias Key = String typealias Value = String init(dictionaryLiteral elements: (Key, Value)...) { for (key, value) in elements where key == "name" { self.name = value } } }
  • 11.
    Literal Convertible (Conti.) extensionSelector: StringLiteralConvertible { typealias StringLiteralType = String init(extendedGraphemeClusterLiteral value: StringLiteralType) { self.value = value } init(unicodeScalarLiteral value: StringLiteralType) { self.value = value } init(stringLiteral value: StringLiteralType) { self.value = value } }
  • 12.
    Literal Convertible (Conti.) •ArrayLiteralConvertible • BooleanLiteralConvertible • DictionaryLiteralConvertible • ExtendedGraphemeClusterLiteralConvertible • FloatLiteralConvertible • IntegerLiteralConvertible • StringLiteralConvertible • StringInterpolationConvertible • UnicodeScalarLiteralConvertible
  • 13.
    Generic func randomInt(integers: [Int])-> Int { let randomIndex = Int(arc4random_uniform(UInt32(integers.count))) return integers[randomIndex] } func randomFloat(floats: [Float]) -> Float { let randomIndex = Int(arc4random_uniform(UInt32(floats.count))) return floats[randomIndex] } func randomString(strings: [String]) -> String { let randomIndex = Int(arc4random_uniform(UInt32(strings.count))) return strings[randomIndex] } func randomElement<T>(elements: [T]) -> T { let randomIndex = Int(arc4random_uniform(UInt32(elements.count))) return elements[randomIndex] }
  • 14.
    Generic struct RandomContainer<T> { letitems: [T] init(items: [T]) { self.items = items } func getRandom() -> T { let randomIndex = Int(arc4random_uniform(UInt32(items.count))) return items[randomIndex] } } let integers = [1,2,3,4,5] let randomer = RandomContainer(items: integers) randomer.getRandom() let names = ["A","B","C"] let namesRandomer = RandomContainer(items: names) nameRandomer.getRandom()
  • 15.
    Operators Operator Description Associativi ty Precedence ++Increment (prefix) - - Decrement (postfix) + Add (infix) Left 140 - Substract (infix) Left 140 * Multiply (infix) Left 150 / Divide (infix) Left 150 ?? Nil Coalescing Right 131 • More than 50 default operators • Three types: prefix, infix, postfix
  • 16.
    Operators (Conti.) let dict= ["name":"Sokna"] let myName = dict["name"] ?? "UnkownName" • nil-coalescing operator (??) : return unwrapped value or an alternative value in the case that the optional value is nil infix operator ?? { associativity right precedence 131 } func ??<T>(lhs: T?, rhs: T) -> T { guard let lhs = lhs else { return rhs} return lhs }
  • 17.
    Operators (Conti.) let optionalString:String? = "optional" • ?: Optional operator: Indicate the possibility that a value might be absent (nil) or not postfix operator ? {} postfix func ?<T>(lhs: T) { Optional.Some(lhs) }
  • 18.
    Others • Value-Oriented Programming •Pattern Matching • Dynamic Dispatch & Whole module optimization • enum • Bridgeable with Objective-C code
  • 19.
    Others (Conti.) • Sever-sideSwift • http://perfect.org • https://github.com/NSNotFound/blog.swift • https://github.com/Zewo • https://github.com/Zewo/Epoch • https://github.com/Zewo/OpenSSL
  • 20.