SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
This are the slides for the talk that I made at Codemotion 2015.
Swift had provided developers with new features of the most modern languages combined with a simple and elegant syntax. In this presentation I explore what an advanced architecture is and its benefits, how design patterns help us in an advanced architecture, and how to implement those patterns in Swift, taking advantage of Swift 2.x new features.
This are the slides for the talk that I made at Codemotion 2015.
Swift had provided developers with new features of the most modern languages combined with a simple and elegant syntax. In this presentation I explore what an advanced architecture is and its benefits, how design patterns help us in an advanced architecture, and how to implement those patterns in Swift, taking advantage of Swift 2.x new features.
13.
#Swift4AA
Programming Paradigms
ObjectOriented
Imperative PrgmDeclarative Prgm
Control
Flow
Data
Flow
14.
#Swift4AA
Is Swift a Functional
Language?
Purity or pragmatism?
Swift allows to express solutions
declaratively
Many standard functions to help and even
more from some libraries
19.
Guard
func presentCell(cell: SpeakerCellProtocol, indexPath:
NSIndexPath) {
let index = indexPath.row
guard index < speakers.count else { return }
let speaker = speakers[index]
cell.displayName(speaker.name)
cell.displayTitle(speaker.title)
cell.displayDateSubmitted(relativeDateStringFromDate(s
peaker.dateSubmitted))
}
20.
#Swift4AA
Swift abstractions:
protocols
Swift doesn’t have abstract classes
Usage of the protocol extensions
Don’t “translate” design patterns to Swift
24.
#Swift4AA
Protocol Extensions
Adds to class API vs subsystem
(composition)
Reuse
DRY
25.
#Swift4AA
Error Handling
Elegant implementation
Extended to current Cocoa for free
Errors subclass ErrorType
26.
var control = Houston(fuel: 1.0,
astronaut: nil, spaceshipOK: true)
do {
try control.launchSpaceship()
} catch Houston.LaunchError.NoFuel
{
// Add Fuel
print("Adding fuel")
} catch
Houston.LaunchError.NoAstronaut {
print("Next in line")
} catch
Houston.LaunchError.BrokenShip(let
problem) {
print(problem)
} catch let unknowError {
//
}
Ready for Life
class Houston {
let fuel: Double
let astronaut: String
let spaceshipOK: Bool
init (fuel: Double, astronaut: String?,
spaceshipOK: Bool) {
self.fuel = fuel
self.astronaut = astronaut ?? ""
self.spaceshipOK = spaceshipOK
}
enum LaunchError: ErrorType {
case NoFuel, NoAstronaut,
BrokenShip(String)
}
func launchSpaceship() throws {
guard fuel >= 1.0 else { throw
LaunchError.NoFuel }
guard astronaut != "" else { throw
LaunchError.NoAstronaut }
guard spaceshipOK else { throw
LaunchError.BrokenShip("Engine") }
print("Launching spaceship")
}
}
27.
#Swift4AA
Generics
Aimed at DRY
Much better that id (Obj-C) or AnyObject/
Any
And use where for further restrictions
28.
Generic Awesome Power
func countGreater<T where T:Comparable>(array:
[T], value: T) -> Int {
var count = 0
for elem in array where elem > value {
count++
}
return count
}
29.
#Swift4AA
If you scratch my back…
It is also true that some patterns make
using Swift easier
Prefer non-optionals: Null Object (and
combine it with errors)
30.
class Text {
func displayContents() {
print("Hola")
}
}
class NullText: Text {
override func
displayContents() {
}
}
func fetchText() -> Text {
return NullText()
}
let text = fetchText()
text.displayContents()
Null Object
class Text {
func displayContents() {
print("Hola")
}
}
func fetchText() -> Text? {
return nil
}
if let text = fetchText() {
text.displayContents()
}
31.
#Swift4AA
Still Pending
Introspection
• Very limited
• Useful for: Dependency Injection, KVC/KVO
Dynamic Dispatching
Community Supported features:
• Promises
• Reactive Extensions
33.
#Swift4AA
Conclusions
Swift is a very expressive language
• Intent
It is mature enough to be used with
advanced architectures
Some additional features would be more
than welcome