├── .gitattributes ├── .gitignore ├── Chapter 01 ├── OOP_Design.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── POP_design.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── hoffmanjon.xcuserdatad │ └── UserInterfaceState.xcuserstate │ └── jonhoffman.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── Chapter 02 ├── Class Hierarchy.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── Enumerations.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── ExtendingType.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── Tuples.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── ValueVsReference.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── ValueVsReference2.playground │ ├── Contents.swift │ ├── contents.xcplayground │ ├── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ └── timeline.xctimeline ├── Chapter 03 ├── GuardExample.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── ReturnValues.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── Swift2ErrorHandling.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── hoffmanjon.xcuserdatad │ └── UserInterfaceState.xcuserstate │ └── jonhoffman.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── Chapter 04 ├── Delegation │ ├── Delegation.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcuserdata │ │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ │ └── UserInterfaceState.xcuserstate │ │ │ │ └── jonhoffman.xcuserdatad │ │ │ │ └── UserInterfaceState.xcuserstate │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ ├── xcdebugger │ │ │ │ └── Breakpoints_v2.xcbkptlist │ │ │ └── xcschemes │ │ │ │ ├── Delegation.xcscheme │ │ │ │ └── xcschememanagement.plist │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── xcschemes │ │ │ ├── Delegation.xcscheme │ │ │ └── xcschememanagement.plist │ ├── Delegation │ │ ├── AppDelegate.swift │ │ ├── Assets.xcassets │ │ │ └── AppIcon.appiconset │ │ │ │ └── Contents.json │ │ ├── Base.lproj │ │ │ ├── LaunchScreen.storyboard │ │ │ └── Main.storyboard │ │ ├── Delegates.swift │ │ ├── Info.plist │ │ ├── Person.swift │ │ ├── Protocols.swift │ │ └── ViewController.swift │ ├── DelegationTests │ │ ├── DelegationTests.swift │ │ └── Info.plist │ └── DelegationUITests │ │ ├── DelegationUITests.swift │ │ └── Info.plist ├── DelegationPlayground.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── QueueProtocol.playground │ ├── Contents.swift │ ├── Sources │ │ └── SupportCode.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── TaeKwonDo.playground │ ├── Contents.swift │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── timeline.xctimeline └── TaeKwonDo_OOP.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── hoffmanjon.xcuserdatad │ └── UserInterfaceState.xcuserstate │ └── jonhoffman.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── Chapter 05 ├── CollectionTypeProtocolExtension.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── DoubleExtension.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── ExtendInt.playground │ ├── Contents.swift │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── timeline.xctimeline ├── ExtendString.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── Subclass_String │ ├── Subclass.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcuserdata │ │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ │ └── UserInterfaceState.xcuserstate │ │ │ │ └── jonhoffman.xcuserdatad │ │ │ │ └── UserInterfaceState.xcuserstate │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── xcschemes │ │ │ │ ├── Subclass.xcscheme │ │ │ │ └── xcschememanagement.plist │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── xcschemes │ │ │ ├── Subclass.xcscheme │ │ │ └── xcschememanagement.plist │ ├── Subclass │ │ ├── AppDelegate.h │ │ ├── AppDelegate.m │ │ ├── Assets.xcassets │ │ │ └── AppIcon.appiconset │ │ │ │ └── Contents.json │ │ ├── Base.lproj │ │ │ ├── LaunchScreen.storyboard │ │ │ └── Main.storyboard │ │ ├── Info.plist │ │ ├── MyString.h │ │ ├── MyString.m │ │ ├── ViewController.h │ │ ├── ViewController.m │ │ └── main.m │ ├── SubclassTests │ │ ├── Info.plist │ │ └── SubclassTests.m │ └── SubclassUITests │ │ ├── Info.plist │ │ └── SubclassUITests.m ├── TaeKwonDo.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── TextValidation.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── TextValidation │ ├── TextValidation.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── xcschemes │ │ │ ├── TextValidation.xcscheme │ │ │ └── xcschememanagement.plist │ │ └── jonhoffman.xcuserdatad │ │ └── xcschemes │ │ ├── TextValidation.xcscheme │ │ └── xcschememanagement.plist │ ├── TextValidation │ ├── AppDelegate.swift │ ├── Assets.xcassets │ │ └── AppIcon.appiconset │ │ │ └── Contents.json │ ├── Base.lproj │ │ ├── LaunchScreen.storyboard │ │ └── Main.storyboard │ ├── Info.plist │ ├── TextValidation.swift │ └── ViewController.swift │ ├── TextValidationTests │ ├── Info.plist │ └── TextValidationTests.swift │ └── TextValidationUITests │ ├── Info.plist │ └── TextValidationUITests.swift ├── Chapter 06 ├── AssociatedType.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── ListProtocol.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ └── jonhoffman.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── Chapter 07 ├── BridgePattern.playground │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── section-1.swift ├── Builder Pattern.playground │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── section-1.swift ├── CommandPattern.playground │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ ├── section-1.swift │ └── timeline.xctimeline ├── FacadePattern.playground │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── section-1.swift ├── FactoryMethodPattern.playground │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── section-1.swift ├── ObserverPattern.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate ├── ProxyPattern.playground │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── section-1.swift ├── SingletonPattern.playground │ ├── contents.xcplayground │ ├── playground.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── section-1.swift └── StrategyPattern.playground │ ├── contents.xcplayground │ ├── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ ├── hoffmanjon.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ │ └── jonhoffman.xcuserdatad │ │ └── UserInterfaceState.xcuserstate │ └── section-1.swift ├── Chapter 08 ├── Calculator │ └── Calculator │ │ ├── Calculator.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcuserdata │ │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ │ └── UserInterfaceState.xcuserstate │ │ │ │ └── jonhoffman.xcuserdatad │ │ │ │ └── UserInterfaceState.xcuserstate │ │ └── xcuserdata │ │ │ ├── hoffmanjon.xcuserdatad │ │ │ └── xcschemes │ │ │ │ ├── Calculator.xcscheme │ │ │ │ └── xcschememanagement.plist │ │ │ └── jonhoffman.xcuserdatad │ │ │ └── xcschemes │ │ │ ├── Calculator.xcscheme │ │ │ └── xcschememanagement.plist │ │ ├── Calculator │ │ ├── AppDelegate.swift │ │ ├── Assets.xcassets │ │ │ └── AppIcon.appiconset │ │ │ │ └── Contents.json │ │ ├── Base.lproj │ │ │ ├── LaunchScreen.storyboard │ │ │ └── Main.storyboard │ │ ├── Calculator.swift │ │ ├── CalculatorCommands.swift │ │ ├── Info.plist │ │ └── ViewController.swift │ │ ├── CalculatorTests │ │ ├── CalculatorTests.swift │ │ └── Info.plist │ │ └── CalculatorUITests │ │ ├── CalculatorUITests.swift │ │ └── Info.plist ├── DataAccessCaseStudy.zip └── Logger.playground │ ├── Contents.swift │ ├── contents.xcplayground │ └── playground.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ ├── hoffmanjon.xcuserdatad │ └── UserInterfaceState.xcuserstate │ └── jonhoffman.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── License └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Chapter 01/OOP_Design.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 01/OOP_Design.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 01/OOP_Design.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 01/OOP_Design.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 01/OOP_Design.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 01/OOP_Design.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 01/POP_design.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 01/POP_design.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 01/POP_design.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 01/POP_design.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 01/POP_design.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 01/POP_design.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/Class Hierarchy.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | class Animal { 4 | var numberOfLegs = 0 5 | 6 | func sleeps() { 7 | print("zzzzz") 8 | } 9 | func walking() { 10 | print("Walking on \(numberOfLegs) legs") 11 | } 12 | func speaking() { 13 | print("No sound") 14 | } 15 | } 16 | 17 | class Biped: Animal { 18 | override init() { 19 | super.init() 20 | numberOfLegs = 2 21 | } 22 | } 23 | 24 | class Quadruped: Animal { 25 | override init() { 26 | super.init() 27 | numberOfLegs = 4 28 | } 29 | } 30 | 31 | class Dog: Quadruped{ 32 | override func speaking() { 33 | print("Barking") 34 | } 35 | } 36 | 37 | 38 | var bipedal = Biped() 39 | var dog = Dog() 40 | 41 | bipedal.walking() 42 | dog.walking() 43 | 44 | -------------------------------------------------------------------------------- /Chapter 02/Class Hierarchy.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 02/Class Hierarchy.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 02/Class Hierarchy.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/Class Hierarchy.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/Class Hierarchy.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/Class Hierarchy.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/Enumerations.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | enum DevicesOrig { 4 | case IPod 5 | case IPhone 6 | case IPad 7 | } 8 | 9 | 10 | enum DevicesRawValues: String { 11 | case IPod = "iPod" 12 | case IPhone = "iPhone" 13 | case IPad = "iPad" 14 | } 15 | 16 | DevicesRawValues.IPod.rawValue 17 | 18 | enum Devices { 19 | case IPod(model: Int, year: Int, memory: Int) 20 | case IPhone(model: String, memory: Int) 21 | case IPad(model: String, memory: Int) 22 | 23 | } 24 | 25 | var myPhone = Devices.IPhone(model: "6", memory: 64) 26 | var myTablet = Devices.IPad(model: "Pro", memory: 128) 27 | 28 | print(myPhone) 29 | 30 | switch myPhone { 31 | case .IPod(let model, let year, let memory): 32 | print("iPod: \(model) \(memory)") 33 | case .IPhone(let model, let memory): 34 | print("iPhone: \(model) \(memory)") 35 | case .IPad(let model, let memory): 36 | print("iPad: \(model) \(memory)") 37 | } 38 | 39 | 40 | 41 | enum Reindeer: String{ 42 | case Dasher, Dancer, Prancer, Vixen, Comet, Cupid, Donner, Blitzen, Rudolph 43 | 44 | static var allCases: [Reindeer] { 45 | return [Dasher, Dancer, Prancer, Vixen, Comet, Cupid, Donner, Blitzen, Rudolph] 46 | } 47 | 48 | static func randomCase() -> Reindeer { 49 | let randomValue = Int(arc4random_uniform(UInt32(allCases.count))) 50 | return allCases[randomValue] 51 | } 52 | } 53 | 54 | print(Reindeer.Rudolph) 55 | print(Reindeer.randomCase()) 56 | 57 | enum BookFormat { 58 | case PaperBack (pageCount: Int, price: Double) 59 | case HardCover (pageCount: Int, price: Double) 60 | case PDF (pageCount: Int, price: Double) 61 | case EPub (pageCount: Int, price: Double) 62 | case Kindle (pageCount: Int, price: Double) 63 | 64 | var pageCount: Int { 65 | switch self { 66 | case .PaperBack(let pageCount, _): 67 | return pageCount 68 | case .HardCover(let pageCount, _): 69 | return pageCount 70 | case .PDF(let pageCount, _): 71 | return pageCount 72 | case .EPub(let pageCount, _): 73 | return pageCount 74 | case .Kindle(let pageCount, _): 75 | return pageCount 76 | } 77 | } 78 | 79 | var price: Double { 80 | switch self { 81 | case .PaperBack(_, let price): 82 | return price 83 | case .HardCover(_, let price): 84 | return price 85 | case .PDF(_, let price): 86 | return price 87 | case .EPub(_, let price): 88 | return price 89 | case .Kindle(_, let price): 90 | return price 91 | } 92 | } 93 | 94 | func purchaseTogether(otherFormat: BookFormat) -> Double { 95 | return (self.price + otherFormat.price) * 0.80 96 | } 97 | } 98 | 99 | var paperBack = BookFormat.PaperBack(pageCount: 220, price: 39.99) 100 | print("\(paperBack.pageCount) - \(paperBack.price)") 101 | var pdf = BookFormat.PDF(pageCount: 180, price: 14.99) 102 | var totoal = paperBack.purchaseTogether(otherFormat: pdf) 103 | 104 | switch paperBack { 105 | case .PaperBack(let pageCount, let price): 106 | print("\(pageCount) - \(price)") 107 | case .HardCover(let pageCount, let price): 108 | print("\(pageCount) - \(price)") 109 | case .PDF(let pageCount, let price): 110 | print("\(pageCount) - \(price)") 111 | case .EPub(let pageCount, let price): 112 | print("\(pageCount) - \(price)") 113 | case .Kindle(let pageCount, let price): 114 | print("\(pageCount) - \(price)") 115 | } 116 | -------------------------------------------------------------------------------- /Chapter 02/Enumerations.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 02/Enumerations.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 02/Enumerations.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/Enumerations.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/Enumerations.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/Enumerations.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/ExtendingType.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | extension Int { 4 | func factorial() -> Int { 5 | var answer = 1 6 | for x in (1...self).reversed() { 7 | answer *= x 8 | } 9 | return answer 10 | } 11 | } 12 | 13 | var f = 10 14 | print(f.factorial()) 15 | -------------------------------------------------------------------------------- /Chapter 02/ExtendingType.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 02/ExtendingType.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 02/ExtendingType.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/ExtendingType.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/ExtendingType.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/ExtendingType.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/Tuples.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | let mathGrade1 = ("Jon", 100) 4 | let (name, score) = mathGrade1 5 | print("\(name) - \(score)") 6 | 7 | let mathGrade2 = (name: "Jon", grade: 100) 8 | print("\(mathGrade2.name) - \(mathGrade2.grade)") 9 | 10 | 11 | func calculateTip(billAmount: Double, 12 | tipPercent: Double) -> 13 | (tipAmount: Double, totalAmount: Double) { 14 | 15 | let tip = billAmount * (tipPercent/100) 16 | let total = billAmount + tip 17 | return (tipAmount: tip, totalAmount: total) 18 | } 19 | 20 | var tip = calculateTip(billAmount: 31.98, tipPercent: 20) 21 | print("\(tip.tipAmount) - \(tip.totalAmount)") 22 | 23 | typealias myTuple = (tipAmount: Double, totalAmount: Double) 24 | -------------------------------------------------------------------------------- /Chapter 02/Tuples.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 02/Tuples.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 02/Tuples.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/Tuples.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/Tuples.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/Tuples.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/ValueVsReference.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | struct MyValueType { 4 | var name: String 5 | var assignment: String 6 | var grade: Int 7 | 8 | } 9 | 10 | class MyReferenceType { 11 | var name: String 12 | var assignment: String 13 | var grade: Int 14 | 15 | init(name: String, assignment: String, grade: Int) { 16 | self.name = name 17 | self.assignment = assignment 18 | self.grade = grade 19 | } 20 | } 21 | 22 | 23 | 24 | func extraCreditReferenceType(ref: MyReferenceType, extraCredit: Int) { 25 | ref.grade += extraCredit 26 | } 27 | 28 | func extraCreditValueType( val: MyValueType, extraCredit: Int) { 29 | var val = val 30 | val.grade += extraCredit 31 | } 32 | 33 | func newExtraCreditValueType( val: inout MyValueType, extraCredit: Int) { 34 | val.grade += extraCredit 35 | } 36 | 37 | 38 | var ref = 39 | MyReferenceType(name: "Jon", assignment: "Math Test 1", grade: 90) 40 | extraCreditReferenceType(ref: ref, extraCredit:5) 41 | print("Reference: \(ref.name) - \(ref.grade)") 42 | 43 | var val = 44 | MyValueType(name: "Jon", assignment: "Math Test 1", grade: 90) 45 | extraCreditValueType(val: val, extraCredit:5) 46 | print("Value: \(val.name) - \(val.grade)") 47 | 48 | var newVal = MyValueType(name: "Jon", assignment: "Math Test 1", grade: 90) 49 | newExtraCreditValueType(val: &newVal, extraCredit:5) 50 | print("Value: \(newVal.name) - \(newVal.grade)") 51 | 52 | 53 | func getGradeForAssignment(assignment: MyReferenceType) { 54 | // Code to get grade from DB 55 | // Random code here to illistrate issue 56 | let num = Int(arc4random_uniform(20) + 80) 57 | assignment.grade = num 58 | print("Grade for \(assignment.name) is \(num)") 59 | } 60 | 61 | var mathGrades = [MyReferenceType]() 62 | var students = ["Jon", "Kim", "Kailey", "Kara"] 63 | var mathAssignment = MyReferenceType(name: "", assignment: "Math Assignment", grade: 0) 64 | 65 | for student in students { 66 | mathAssignment.name = student 67 | getGradeForAssignment(assignment: mathAssignment) 68 | mathGrades.append(mathAssignment) 69 | } 70 | 71 | for assignment in mathGrades { 72 | print("\(assignment.name): grade \(assignment.grade)") 73 | } 74 | 75 | func getGradeForAssignment( assignment: inout MyValueType) { 76 | // Code to get grade from DB 77 | // Random code here to illistrate issue 78 | let num = Int(arc4random_uniform(20) + 80) 79 | assignment.grade = num 80 | print("Grade for \(assignment.name) is \(num)") 81 | } 82 | 83 | var mathGrades2 = [MyValueType]() 84 | //var students = ["Jon", "Kim", "Kailey", "Kara"] 85 | var mathAssignment2 = MyValueType(name: "", assignment: "Math Assignment", grade: 0) 86 | 87 | for student in students { 88 | mathAssignment2.name = student 89 | getGradeForAssignment(assignment: &mathAssignment2) 90 | mathGrades2.append(mathAssignment2) 91 | } 92 | 93 | for assignment in mathGrades2 { 94 | print("\(assignment.name): grade \(assignment.grade)") 95 | } 96 | -------------------------------------------------------------------------------- /Chapter 02/ValueVsReference.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 02/ValueVsReference.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 02/ValueVsReference.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/ValueVsReference.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/ValueVsReference.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/ValueVsReference.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/ValueVsReference2.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | // Throws an error because we can not create 4 | // A linked list from a value type 5 | /* 6 | struct LinkedListValueType { 7 | var value: String 8 | var next: LinkedListValueType? 9 | } 10 | */ 11 | 12 | class LinkedListReferenceType { 13 | var value: String 14 | var next: LinkedListReferenceType? 15 | 16 | init(value: String) { 17 | self.value = value 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter 02/ValueVsReference2.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 02/ValueVsReference2.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 02/ValueVsReference2.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/ValueVsReference2.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/ValueVsReference2.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 02/ValueVsReference2.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 02/ValueVsReference2.playground/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 03/GuardExample.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | struct Blog { 4 | var author: String? 5 | var name: String? 6 | var url: NSURL? 7 | } 8 | 9 | var myBlog = Blog(author: "Jon Hoffman", name: "Mastering Swift", url: NSURL(string: "http://masteringswift.blogspot.com")) 10 | 11 | 12 | func blogInfo(blog: Blog?) { 13 | if let blog = blog { 14 | if let author = blog.author, let name = blog.name{ 15 | print("BLOG:") 16 | print(" Author: \(author)") 17 | print(" name: \(name)") 18 | } else { 19 | print("Author or name is nil") 20 | } 21 | } else { 22 | print("Blog is nil") 23 | } 24 | } 25 | 26 | func blogInfo2(blog: Blog?) { 27 | guard let blog = blog else { 28 | print("Blog is nil") 29 | return 30 | } 31 | 32 | guard let author = blog.author, let name = blog.name else { 33 | print("Author or name is nil") 34 | return 35 | } 36 | 37 | print("BLOG:") 38 | print(" Author: \(author)") 39 | print(" name: \(name)") 40 | } 41 | -------------------------------------------------------------------------------- /Chapter 03/GuardExample.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 03/GuardExample.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 03/GuardExample.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 03/GuardExample.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 03/GuardExample.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 03/GuardExample.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 03/ReturnValues.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | enum DrinkSize { 4 | case Can12 5 | case Can16 6 | case Can24 7 | case Can32 8 | } 9 | 10 | enum DrinkTemperature { 11 | case TooHot 12 | case TooCold 13 | case JustRight 14 | } 15 | 16 | 17 | 18 | struct Drink { 19 | var volume: Double 20 | var caffeine: Double 21 | var temperature: Double 22 | var drinkSize: DrinkSize 23 | var description: String 24 | 25 | mutating func drinking(amount: Double) -> Bool { 26 | guard amount < volume else { 27 | return false 28 | } 29 | volume -= amount 30 | return true 31 | } 32 | mutating func temperatureChange(change: Double) -> DrinkTemperature { 33 | temperature += change 34 | guard temperature > 35 else { 35 | return .TooCold 36 | } 37 | guard temperature < 45 else { 38 | return .TooHot 39 | } 40 | return .JustRight 41 | } 42 | } 43 | 44 | 45 | var myDrink = Drink(volume: 23.5, caffeine: 280, 46 | temperature: 38.2, drinkSize: DrinkSize.Can24, 47 | description: "Drink Structure") 48 | 49 | if myDrink.drinking(amount: 50.0) { 50 | print("Had a drink") 51 | } else { 52 | print("Error") 53 | } 54 | 55 | var results = myDrink.temperatureChange(change: -5) 56 | switch results { 57 | case .TooHot: 58 | print("Drink too hot") 59 | case .TooCold: 60 | print("Drink too cold") 61 | case .JustRight: 62 | print("Drink just right") 63 | } 64 | -------------------------------------------------------------------------------- /Chapter 03/ReturnValues.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 03/ReturnValues.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 03/ReturnValues.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 03/ReturnValues.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 03/ReturnValues.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 03/ReturnValues.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 03/Swift2ErrorHandling.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | enum DrinkSize { 4 | case Can12 5 | case Can16 6 | case Can24 7 | case Can32 8 | } 9 | 10 | enum DrinkErrors3: Error { 11 | case insufficentVolume 12 | case tooHot 13 | case tooCold 14 | } 15 | 16 | enum DrinkErrors2: Error { 17 | case insufficentVolume 18 | case tempOutOfRange (Description: String) 19 | } 20 | 21 | enum DrinkErrors: Error { 22 | case insufficentVolume 23 | case tooHot (temp: Double) 24 | case tooCold (temp: Double) 25 | } 26 | 27 | struct Drink { 28 | var volume: Double 29 | var caffeine: Double 30 | var temperature: Double 31 | var drinkSize: DrinkSize 32 | var description: String 33 | 34 | 35 | /// This method will take a drink from our drink if we have 36 | /// enough liquid left in our drink. 37 | 38 | /// - parameter amount: The amount to drink 39 | /// - throws: DrinkError.insufficentVolume if there is not 40 | /// enough volume left 41 | mutating func drinking(amount: Double) throws { 42 | guard amount < volume else { 43 | throw DrinkErrors.insufficentVolume 44 | } 45 | volume -= amount 46 | } 47 | 48 | 49 | /// This method will change the temperature of the drink. 50 | /// 51 | /// - parameter change: The amount to change, can be negative 52 | /// or positive 53 | /// 54 | /// - throws: 55 | /// - DrinkError.tooHot if the drink is to hot 56 | /// - DrinkError.tooCold if the drink is to cold 57 | /// 58 | 59 | mutating func temperatureChange(change: Double) throws { 60 | temperature += change 61 | guard temperature > 35 else { 62 | throw DrinkErrors.tooCold(temp: temperature) 63 | } 64 | guard temperature < 45 else { 65 | throw DrinkErrors.tooHot(temp: temperature) 66 | } 67 | } 68 | } 69 | 70 | 71 | var myDrink = Drink(volume: 23.5, caffeine: 280, 72 | temperature: 38.2, drinkSize: DrinkSize.Can24, 73 | description: "Drink Structure") 74 | 75 | do { 76 | try myDrink.drinking(amount: 50.0) 77 | print("Took Drink") 78 | } catch DrinkErrors.insufficentVolume{ 79 | print("Error taking drink") 80 | } 81 | 82 | do { 83 | try myDrink.temperatureChange(change: 20.0) 84 | } catch DrinkErrors.tooHot(let temp) { 85 | print("Drink too hot: \(temp) degrees ") 86 | } catch DrinkErrors.tooCold(let temp) { 87 | print("Drink too cold: \(temp) degrees ") 88 | } 89 | 90 | try? myDrink.temperatureChange(change: 20.0) -------------------------------------------------------------------------------- /Chapter 03/Swift2ErrorHandling.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 03/Swift2ErrorHandling.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 03/Swift2ErrorHandling.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 03/Swift2ErrorHandling.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 03/Swift2ErrorHandling.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 03/Swift2ErrorHandling.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation.xcodeproj/project.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 04/Delegation/Delegation.xcodeproj/project.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation.xcodeproj/project.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 04/Delegation/Delegation.xcodeproj/project.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation.xcodeproj/xcuserdata/hoffmanjon.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation.xcodeproj/xcuserdata/hoffmanjon.xcuserdatad/xcschemes/Delegation.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 95 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation.xcodeproj/xcuserdata/hoffmanjon.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Delegation.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 34249F4D1BF9604500C14A92 16 | 17 | primary 18 | 19 | 20 | 34249F611BF9604600C14A92 21 | 22 | primary 23 | 24 | 25 | 34249F6C1BF9604600C14A92 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation.xcodeproj/xcuserdata/jonhoffman.xcuserdatad/xcschemes/Delegation.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 95 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation.xcodeproj/xcuserdata/jonhoffman.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Delegation.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 34249F4D1BF9604500C14A92 16 | 17 | primary 18 | 19 | 20 | 34249F611BF9604600C14A92 21 | 22 | primary 23 | 24 | 25 | 34249F6C1BF9604600C14A92 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Delegation 4 | // 5 | // Created by Jon Hoffman on 11/15/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation/Delegates.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Delegates.swift 3 | // Delegation 4 | // 5 | // Created by Jon Hoffman on 11/15/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol DisplayNameDelegate { 12 | func displayName(_ name: String) 13 | } 14 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation/Person.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Person.swift 3 | // Delegation 4 | // 5 | // Created by Jon Hoffman on 11/15/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | struct Person: FullName{ 12 | var firstName = "" { 13 | didSet { 14 | displayNameDelegate.displayName(getFullName()) 15 | } 16 | } 17 | var lastName = "" { 18 | didSet { 19 | displayNameDelegate.displayName(getFullName()) 20 | } 21 | } 22 | var displayNameDelegate: DisplayNameDelegate 23 | 24 | init(displayNameDelegate: DisplayNameDelegate) { 25 | self.displayNameDelegate = displayNameDelegate 26 | } 27 | 28 | func getFullName() -> String { 29 | return "\(firstName) \(lastName)" 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation/Protocols.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Protocols.swift 3 | // Delegation 4 | // 5 | // Created by Jon Hoffman on 11/15/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol FullName { 12 | var firstName: String {get set} 13 | var lastName: String {get set} 14 | var displayNameDelegate: DisplayNameDelegate {get set} 15 | } 16 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/Delegation/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // Delegation 4 | // 5 | // Created by Jon Hoffman on 11/15/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController, DisplayNameDelegate { 12 | 13 | @IBOutlet weak var firstNameTextField: UITextField? 14 | @IBOutlet weak var lastNameTextField: UITextField? 15 | @IBOutlet weak var fullNameLabel: UILabel? 16 | 17 | var person: Person? 18 | 19 | override func viewDidLoad() { 20 | super.viewDidLoad() 21 | 22 | person = Person(displayNameDelegate: self) 23 | } 24 | 25 | override func didReceiveMemoryWarning() { 26 | super.didReceiveMemoryWarning() 27 | // Dispose of any resources that can be recreated. 28 | } 29 | 30 | @IBAction func textFieldDidChange(_ textField: UITextField) { 31 | if let first = firstNameTextField?.text, let last = lastNameTextField?.text { 32 | person?.firstName = first 33 | person?.lastName = last 34 | } 35 | } 36 | 37 | func displayName(_ name: String) { 38 | fullNameLabel?.text = "\(name)" 39 | } 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/DelegationTests/DelegationTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DelegationTests.swift 3 | // DelegationTests 4 | // 5 | // Created by Jon Hoffman on 11/15/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import Delegation 11 | 12 | class DelegationTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | func testExample() { 25 | // This is an example of a functional test case. 26 | // Use XCTAssert and related functions to verify your tests produce the correct results. 27 | } 28 | 29 | func testPerformanceExample() { 30 | // This is an example of a performance test case. 31 | self.measure { 32 | // Put the code you want to measure the time of here. 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/DelegationTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/DelegationUITests/DelegationUITests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DelegationUITests.swift 3 | // DelegationUITests 4 | // 5 | // Created by Jon Hoffman on 11/15/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | 11 | class DelegationUITests: XCTestCase { 12 | 13 | override func setUp() { 14 | super.setUp() 15 | 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | 18 | // In UI tests it is usually best to stop immediately when a failure occurs. 19 | continueAfterFailure = false 20 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 21 | XCUIApplication().launch() 22 | 23 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 24 | } 25 | 26 | override func tearDown() { 27 | // Put teardown code here. This method is called after the invocation of each test method in the class. 28 | super.tearDown() 29 | } 30 | 31 | func testExample() { 32 | // Use recording to get started writing UI tests. 33 | // Use XCTAssert and related functions to verify your tests produce the correct results. 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Chapter 04/Delegation/DelegationUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter 04/DelegationPlayground.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | protocol DisplayNameDelegate { 4 | func displayName(name: String) 5 | } 6 | 7 | struct Person { 8 | var firstName = "" { 9 | didSet { 10 | displayNameDelegate.displayName(name: getFullName()) 11 | } 12 | } 13 | var lastName = "" { 14 | didSet { 15 | displayNameDelegate.displayName(name: getFullName()) 16 | } 17 | } 18 | var displayNameDelegate: DisplayNameDelegate 19 | 20 | init(displayNameDelegate: DisplayNameDelegate) { 21 | self.displayNameDelegate = displayNameDelegate 22 | } 23 | 24 | func getFullName() -> String { 25 | return "\(firstName) \(lastName)" 26 | } 27 | 28 | } 29 | 30 | struct MyDisplayNameDelegate: DisplayNameDelegate { 31 | 32 | func displayName(name: String) { 33 | print("Name: \(name)") 34 | } 35 | } 36 | 37 | var displayDelegate = MyDisplayNameDelegate() 38 | var person = Person(displayNameDelegate: displayDelegate) 39 | 40 | person.firstName = "Jon" 41 | person.lastName = "Hoffman" 42 | -------------------------------------------------------------------------------- /Chapter 04/DelegationPlayground.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 04/DelegationPlayground.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 04/DelegationPlayground.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 04/DelegationPlayground.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 04/DelegationPlayground.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 04/DelegationPlayground.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 04/QueueProtocol.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | protocol QueueProtocol { 4 | associatedtype QueueType 5 | mutating func addItem(item: QueueType) 6 | mutating func getItem() -> QueueType? 7 | func count() -> Int 8 | } 9 | 10 | 11 | struct IntQueue: QueueProtocol { 12 | var items = [Int]() 13 | 14 | mutating func addItem(item: Int) { 15 | items.append(item) 16 | } 17 | 18 | mutating func getItem() -> Int? { 19 | if items.count > 0 { 20 | return items.remove(at: 0) 21 | } 22 | else { 23 | return nil 24 | } 25 | } 26 | 27 | func count() -> Int { 28 | return items.count 29 | } 30 | } 31 | 32 | 33 | class GenericQueue: QueueProtocol { 34 | var items = [T]() 35 | 36 | func addItem(item: T) { 37 | items.append(item) 38 | } 39 | 40 | func getItem() -> T? { 41 | if items.count > 0 { 42 | return items.remove(at: 0) 43 | } else { 44 | return nil 45 | } 46 | } 47 | 48 | func count() -> Int { 49 | return items.count 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Chapter 04/QueueProtocol.playground/Sources/SupportCode.swift: -------------------------------------------------------------------------------- 1 | // 2 | // This file (and all other Swift source files in the Sources directory of this playground) will be precompiled into a framework which is automatically made available to MyPlayground.playground. 3 | // 4 | -------------------------------------------------------------------------------- /Chapter 04/QueueProtocol.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 04/QueueProtocol.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 04/QueueProtocol.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 04/QueueProtocol.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 04/QueueProtocol.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 04/QueueProtocol.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 04/TaeKwonDo.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | enum TKDBeltColors: Int { 4 | case White, Yellow, Orage, Green, Blue, Purple, Red, FirstDegreeBlack, SecondDegreeBlack, ThirdDegreeBlack, ForthDegreeBlack 5 | } 6 | 7 | enum TKDColorRank: Int { 8 | case NoRank, Probationary, Decided, Intermediate, Senior 9 | } 10 | 11 | protocol TKDRank { 12 | var color: TKDBeltColors {get} 13 | var rank: TKDColorRank {get} 14 | } 15 | 16 | 17 | typealias BoardBreak = (name: String, required: Bool) 18 | 19 | protocol BoardBreakProtocol { 20 | var boardBreaks: [BoardBreak] {get} 21 | var boardBreaksRequired: Int {get} 22 | } 23 | 24 | protocol Form { 25 | var formName: String {get} 26 | } 27 | 28 | protocol FocusPad { 29 | var focusPadDrills: [String] {get} 30 | var focusPadMissesAllowed: Int {get} 31 | } 32 | 33 | protocol OneSteps { 34 | var oneStepsNumbers: [Int] {get} 35 | } 36 | 37 | protocol Sparring { 38 | var sparringRoundsRequired: Int {get} 39 | } 40 | 41 | 42 | 43 | struct WhiteBelt: TKDRank, Form, OneSteps, FocusPad { 44 | let color = TKDBeltColors.White 45 | let rank = TKDColorRank.NoRank 46 | let formName = "Chon-Ji" 47 | let oneStepsNumbers = [1,2,3] 48 | let focusPadDrills = ["Reverse Punch", "Number 1 Front Kick"] 49 | let focusPadMissesAllowed = 2 50 | } 51 | 52 | struct GreenBeltSenior: TKDRank, Form, BoardBreakProtocol, FocusPad, Sparring { 53 | let color = TKDBeltColors.Green 54 | let rank = TKDColorRank.NoRank 55 | let formName = "Do-San" 56 | let focusPadDrills = ["Back Fist", "Number 2 Crescent"] 57 | let focusPadMissesAllowed = 2 58 | let boardBreaks = [(name:"Hammer Fist", required: false), (name:"Front Kick", required: false)] 59 | let boardBreaksRequired = 1 60 | let sparringRoundsRequired = 2 61 | } 62 | 63 | 64 | 65 | 66 | var t = WhiteBelt() 67 | 68 | if let r = t as? BoardBreak { 69 | print("Success") 70 | } 71 | -------------------------------------------------------------------------------- /Chapter 04/TaeKwonDo.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 04/TaeKwonDo.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 04/TaeKwonDo.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 04/TaeKwonDo.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 04/TaeKwonDo.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 04/TaeKwonDo.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 04/TaeKwonDo.playground/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 04/TaeKwonDo_OOP.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | enum TKDBeltColors: Int { 4 | case White, Yellow, Orage, Green, Blue, Purple, Red, FirstDegreeBlack, SecondDegreeBlack, ThirdDegreeBlack, ForthDegreeBlack 5 | } 6 | 7 | enum TKDColorRank: Int { 8 | case NoRank, Probationary, Decided, Intermediate, Senior 9 | } 10 | 11 | typealias BoardBreak = (name: String, required: Bool) 12 | 13 | class TKDTestingRequirements { 14 | var color = TKDBeltColors.White 15 | var rank = TKDColorRank.NoRank 16 | var formName = "" 17 | var focusPadDrills = [String]() 18 | var focusPadMissesAllowed = 2 19 | var sparringRoundsRequired = 0 20 | var boardBreaksRequired = 0 21 | 22 | var boardBreaks: [BoardBreak]? 23 | var oneStepsNumbers: [Int]? 24 | } 25 | 26 | class WhiteBelt: TKDTestingRequirements { 27 | override init () { 28 | super.init() 29 | color = TKDBeltColors.White 30 | rank = TKDColorRank.NoRank 31 | formName = "Chon-Ji" 32 | oneStepsNumbers = [1,2,3] 33 | focusPadDrills = ["Reverse Punch", "Number 1 Front Kick"] 34 | focusPadMissesAllowed = 2 35 | boardBreaksRequired = 0 36 | } 37 | } 38 | 39 | class GreenBeltSenior: TKDTestingRequirements { 40 | override init() { 41 | super.init() 42 | color = TKDBeltColors.Green 43 | rank = TKDColorRank.NoRank 44 | formName = "Do-San" 45 | focusPadDrills = ["Back Fist", "Number 2 Crescent"] 46 | focusPadMissesAllowed = 2 47 | boardBreaks = [(name:"Hammer Fist", required: false), (name:"Front Kick", required: false)] 48 | boardBreaksRequired = 1 49 | sparringRoundsRequired = 2 50 | oneStepsNumbers = nil 51 | } 52 | } -------------------------------------------------------------------------------- /Chapter 04/TaeKwonDo_OOP.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 04/TaeKwonDo_OOP.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 04/TaeKwonDo_OOP.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 04/TaeKwonDo_OOP.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 04/TaeKwonDo_OOP.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 04/TaeKwonDo_OOP.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/CollectionTypeProtocolExtension.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | extension Collection where Self: ExpressibleByArrayLiteral { 4 | func evenElements() -> [Generator.Element] { 5 | 6 | var index = self.startIndex 7 | var result: [Generator.Element] = [] 8 | var i = 0 9 | repeat { 10 | if i % 2 == 0 { 11 | result.append(self[index]) 12 | } 13 | index = self.index(after: index) 14 | i += 1 15 | } while (index != self.endIndex) 16 | 17 | return result 18 | } 19 | 20 | func shuffle() -> [Generator.Element] { 21 | return sorted(){ left, right in 22 | return arc4random() < arc4random() 23 | } 24 | } 25 | } 26 | 27 | 28 | 29 | var origArray = [1,2,3,4,5,6,7,8,9,10] 30 | 31 | var newArray = origArray.evenElements() 32 | var ranArray = origArray.shuffle() 33 | 34 | 35 | var origDict = [1:"One",2:"Two",3:"Three",4:"Four"] 36 | 37 | //Dictionary does not confrom to the ArrayLiteralConvertible protocol 38 | //therefore it does note receive the extension 39 | /* 40 | var new = origDict.evenElements() 41 | 42 | if new is Dictionary { 43 | print("is Dictionary") 44 | } else if new is Array { 45 | print("Is Array") 46 | } 47 | 48 | for item in new { 49 | print(item) 50 | } 51 | */ 52 | -------------------------------------------------------------------------------- /Chapter 05/CollectionTypeProtocolExtension.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 05/CollectionTypeProtocolExtension.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 05/CollectionTypeProtocolExtension.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/CollectionTypeProtocolExtension.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/CollectionTypeProtocolExtension.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/CollectionTypeProtocolExtension.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/DoubleExtension.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | extension Double { 4 | mutating func currencyString() -> String { 5 | let divisor = pow(10.0, 2.0) 6 | let num = (self * divisor).rounded() / divisor 7 | return "$\(num)" 8 | } 9 | } 10 | 11 | var num = 3.45678 12 | print(num.currencyString()) 13 | -------------------------------------------------------------------------------- /Chapter 05/DoubleExtension.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 05/DoubleExtension.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 05/DoubleExtension.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/DoubleExtension.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/DoubleExtension.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/DoubleExtension.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/ExtendInt.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | extension Int { 4 | func squared() -> Int { 5 | return self * self 6 | } 7 | } 8 | 9 | var i = 21 10 | print(i.squared()) 11 | 12 | 13 | extension Int { 14 | var squared1: Int { 15 | return self * self 16 | } 17 | } 18 | 19 | print(i.squared1) 20 | -------------------------------------------------------------------------------- /Chapter 05/ExtendInt.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 05/ExtendInt.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 05/ExtendInt.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/ExtendInt.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/ExtendInt.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/ExtendInt.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/ExtendInt.playground/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 05/ExtendString.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | extension String { 4 | func getFirstChar() -> Character? { 5 | guard self.characters.count > 0 else { 6 | return nil 7 | } 8 | return self[startIndex] 9 | } 10 | 11 | subscript (r: CountableClosedRange) -> String 12 | { 13 | get { 14 | let start = self.index(self.startIndex, offsetBy:r.lowerBound) 15 | let end = self.index(self.startIndex, offsetBy:r.upperBound) 16 | return self.substring(with: start.. 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 05/ExtendString.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 05/ExtendString.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/ExtendString.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/ExtendString.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/ExtendString.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass.xcodeproj/project.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/Subclass_String/Subclass.xcodeproj/project.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass.xcodeproj/project.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/Subclass_String/Subclass.xcodeproj/project.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass.xcodeproj/xcuserdata/hoffmanjon.xcuserdatad/xcschemes/Subclass.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 95 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass.xcodeproj/xcuserdata/hoffmanjon.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Subclass.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 3498A70D1C05368500553D68 16 | 17 | primary 18 | 19 | 20 | 3498A7261C05368600553D68 21 | 22 | primary 23 | 24 | 25 | 3498A7311C05368600553D68 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass.xcodeproj/xcuserdata/jonhoffman.xcuserdatad/xcschemes/Subclass.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 95 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass.xcodeproj/xcuserdata/jonhoffman.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Subclass.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 3498A70D1C05368500553D68 16 | 17 | primary 18 | 19 | 20 | 3498A7261C05368600553D68 21 | 22 | primary 23 | 24 | 25 | 3498A7311C05368600553D68 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // Subclass 4 | // 5 | // Created by Jon Hoffman on 11/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // Subclass 4 | // 5 | // Created by Jon Hoffman on 11/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass/MyString.h: -------------------------------------------------------------------------------- 1 | // 2 | // MyString.h 3 | // Subclass 4 | // 5 | // Created by Jon Hoffman on 11/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MyString : NSString 12 | 13 | -(unichar)getFirstCharacter; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass/MyString.m: -------------------------------------------------------------------------------- 1 | // 2 | // MyString.m 3 | // Subclass 4 | // 5 | // Created by Jon Hoffman on 11/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | #import "MyString.h" 10 | 11 | @interface MyString () 12 | @property (nonatomic, strong) NSString *stringHolder; 13 | @end 14 | 15 | @implementation MyString: NSString 16 | 17 | - (instancetype)initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer { 18 | self = [super init]; 19 | if (self) { 20 | self.stringHolder = [[NSString alloc] initWithCharactersNoCopy:characters length:length freeWhenDone:freeBuffer]; 21 | } 22 | return self; 23 | } 24 | 25 | - (NSUInteger)length { 26 | return self.stringHolder.length; 27 | } 28 | 29 | - (unichar)characterAtIndex:(NSUInteger)index { 30 | return [self.stringHolder characterAtIndex:index]; 31 | } 32 | 33 | 34 | -(unichar)getFirstCharacter { 35 | 36 | return [self.stringHolder characterAtIndex:0]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // Subclass 4 | // 5 | // Created by Jon Hoffman on 11/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // Subclass 4 | // 5 | // Created by Jon Hoffman on 11/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | @interface ViewController () 12 | 13 | @end 14 | 15 | @implementation ViewController 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | // Do any additional setup after loading the view, typically from a nib. 20 | } 21 | 22 | - (void)didReceiveMemoryWarning { 23 | [super didReceiveMemoryWarning]; 24 | // Dispose of any resources that can be recreated. 25 | } 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/Subclass/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // Subclass 4 | // 5 | // Created by Jon Hoffman on 11/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/SubclassTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/SubclassTests/SubclassTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // SubclassTests.m 3 | // SubclassTests 4 | // 5 | // Created by Jon Hoffman on 11/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SubclassTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation SubclassTests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | // Put setup code here. This method is called before the invocation of each test method in the class. 20 | } 21 | 22 | - (void)tearDown { 23 | // Put teardown code here. This method is called after the invocation of each test method in the class. 24 | [super tearDown]; 25 | } 26 | 27 | - (void)testExample { 28 | // This is an example of a functional test case. 29 | // Use XCTAssert and related functions to verify your tests produce the correct results. 30 | } 31 | 32 | - (void)testPerformanceExample { 33 | // This is an example of a performance test case. 34 | [self measureBlock:^{ 35 | // Put the code you want to measure the time of here. 36 | }]; 37 | } 38 | 39 | @end 40 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/SubclassUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter 05/Subclass_String/SubclassUITests/SubclassUITests.m: -------------------------------------------------------------------------------- 1 | // 2 | // SubclassUITests.m 3 | // SubclassUITests 4 | // 5 | // Created by Jon Hoffman on 11/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SubclassUITests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation SubclassUITests 16 | 17 | - (void)setUp { 18 | [super setUp]; 19 | 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | 22 | // In UI tests it is usually best to stop immediately when a failure occurs. 23 | self.continueAfterFailure = NO; 24 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 25 | [[[XCUIApplication alloc] init] launch]; 26 | 27 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 28 | } 29 | 30 | - (void)tearDown { 31 | // Put teardown code here. This method is called after the invocation of each test method in the class. 32 | [super tearDown]; 33 | } 34 | 35 | - (void)testExample { 36 | // Use recording to get started writing UI tests. 37 | // Use XCTAssert and related functions to verify your tests produce the correct results. 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /Chapter 05/TaeKwonDo.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | enum TKDBeltColors: Int { 4 | case White, Yellow, Orage, Green, Blue, Purple, Red, FirstDegreeBlack, SecondDegreeBlack, ThirdDegreeBlack, ForthDegreeBlack 5 | } 6 | 7 | enum TKDColorRank: Int { 8 | case NoRank, Probationary, Decided, Intermediate, Senior 9 | } 10 | 11 | protocol TKDRank { 12 | var color: TKDBeltColors {get} 13 | var rank: TKDColorRank {get} 14 | var passFailTests: [String:Bool] {get set} 15 | } 16 | 17 | extension TKDRank { 18 | mutating func setPassFail(testName: String, pass: Bool) { 19 | passFailTests[testName] = pass 20 | } 21 | } 22 | 23 | 24 | typealias BoardBreak = (name: String, required: Bool) 25 | 26 | protocol BoardBreakProtocol { 27 | var boardBreaks: [BoardBreak] {get} 28 | var boardBreaksRequired: Int {get} 29 | } 30 | 31 | protocol Form { 32 | var formName: String {get} 33 | } 34 | 35 | protocol FocusPad { 36 | var focusPadDrills: [String] {get} 37 | var focusPadMissesAllowed: Int {get} 38 | } 39 | 40 | protocol OneSteps { 41 | var oneStepsNumbers: [Int] {get} 42 | } 43 | 44 | protocol Sparring { 45 | var sparringRoundsRequired: Int {get} 46 | } 47 | 48 | 49 | 50 | struct WhiteBelt: TKDRank, Form, OneSteps, FocusPad { 51 | let color = TKDBeltColors.White 52 | let rank = TKDColorRank.NoRank 53 | let formName = "Chon-Ji" 54 | let oneStepsNumbers = [1,2,3] 55 | let focusPadDrills = ["Reverse Punch", "Number 1 Front Kick"] 56 | let focusPadMissesAllowed = 2 57 | var passFailTests = [String:Bool]() 58 | } 59 | 60 | struct GreenBeltSenior: TKDRank, Form, BoardBreakProtocol, FocusPad, Sparring { 61 | let color = TKDBeltColors.Green 62 | let rank = TKDColorRank.NoRank 63 | let formName = "Do-San" 64 | let focusPadDrills = ["Back Fist", "Number 2 Crescent"] 65 | let focusPadMissesAllowed = 2 66 | let boardBreaks = [(name:"Hammer Fist", required: false), (name:"Front Kick", required: false)] 67 | let boardBreaksRequired = 1 68 | let sparringRoundsRequired = 2 69 | var passFailTests = [String:Bool]() 70 | } 71 | 72 | 73 | 74 | 75 | var t = WhiteBelt() 76 | t.setPassFail(testName: "Form", pass: true) 77 | 78 | if let r = t as? BoardBreak { 79 | print("Success") 80 | } 81 | -------------------------------------------------------------------------------- /Chapter 05/TaeKwonDo.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 05/TaeKwonDo.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 05/TaeKwonDo.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/TaeKwonDo.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/TaeKwonDo.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/TaeKwonDo.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/TextValidation.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | protocol TextValidationProtocol { 4 | 5 | var regExMatchingString: String {get} 6 | var regExFindMatchString: String {get} 7 | var validationMessage: String {get} 8 | 9 | func validateString(str: String) -> Bool 10 | func getMatchingString(str: String) -> String? 11 | 12 | } 13 | 14 | struct AlphaValidation1: TextValidationProtocol { 15 | static let sharedInstance = AlphaValidation1() 16 | private init(){} 17 | 18 | let regExFindMatchString = "^[a-zA-Z]{0,10}" 19 | let validationMessage = "Can only contain Alpha characters" 20 | 21 | var regExMatchingString: String { get { 22 | return regExFindMatchString + "$" 23 | } 24 | } 25 | 26 | func validateString(str: String) -> Bool { 27 | if let _ = str.range(of: regExMatchingString, options: .regularExpression){ 28 | return true 29 | } else { 30 | return false 31 | } 32 | } 33 | func getMatchingString(str: String) -> String? { 34 | if let newMatch = str.range(of: regExFindMatchString, options: .regularExpression) { 35 | return str.substring(with: newMatch) 36 | } else { 37 | return nil 38 | } 39 | } 40 | } 41 | 42 | 43 | extension TextValidationProtocol { 44 | 45 | var regExMatchingString: String { get { 46 | return regExFindMatchString + "$" 47 | } 48 | } 49 | 50 | func validateString(str: String) -> Bool { 51 | if let _ = str.range(of: regExMatchingString, options: .regularExpression){ 52 | return true 53 | } else { 54 | return false 55 | } 56 | } 57 | func getMatchingString(str: String) -> String? { 58 | if let newMatch = str.range(of: regExFindMatchString, options: .regularExpression) { 59 | return str.substring(with: newMatch) 60 | } else { 61 | return nil 62 | } 63 | } 64 | } 65 | 66 | 67 | struct AlphaValidation: TextValidationProtocol { 68 | static let sharedInstance = AlphaValidation() 69 | private init(){} 70 | 71 | let regExFindMatchString = "^[a-zA-Z]{0,10}" 72 | let validationMessage = "Can only contain Alpha characters" 73 | } 74 | 75 | struct AlphaNumericValidation: TextValidationProtocol { 76 | static let sharedInstance = AlphaNumericValidation() 77 | private init(){} 78 | 79 | let regExFindMatchString = "^[a-zA-Z0-9]{0,15}" 80 | let validationMessage = "Can only contain Alpha Numeric characters" 81 | } 82 | 83 | 84 | struct DisplayNameValidation: TextValidationProtocol { 85 | static let sharedInstance = DisplayNameValidation() 86 | private init(){} 87 | 88 | let regExFindMatchString = "^[\\s?[a-zA-Z0-9\\-_\\s]]{0,15}" 89 | let validationMessage = "Display Name can contain a maximum of 15 Alphanumeric Characters" 90 | } 91 | 92 | 93 | var validation = AlphaValidation.sharedInstance 94 | 95 | validation.getMatchingString(str: "t1gh") 96 | validation.validateString(str: "t1gh") 97 | 98 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/TextValidation.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/TextValidation.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/TextValidation.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidation.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidation.xcodeproj/project.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/TextValidation/TextValidation.xcodeproj/project.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidation.xcodeproj/project.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 05/TextValidation/TextValidation.xcodeproj/project.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidation.xcodeproj/xcuserdata/hoffmanjon.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | TextValidation.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 345C2AED1B7A5D7E00EF0935 16 | 17 | primary 18 | 19 | 20 | 345C2B011B7A5D7E00EF0935 21 | 22 | primary 23 | 24 | 25 | 345C2B0C1B7A5D7E00EF0935 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidation.xcodeproj/xcuserdata/jonhoffman.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | TextValidation.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 345C2AED1B7A5D7E00EF0935 16 | 17 | primary 18 | 19 | 20 | 345C2B011B7A5D7E00EF0935 21 | 22 | primary 23 | 24 | 25 | 345C2B0C1B7A5D7E00EF0935 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidation/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // TextValidation 4 | // 5 | // Created by Jon Hoffman on 8/11/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidation/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidation/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidation/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidation/TextValidation.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TextValidation.swift 3 | // TextValidation 4 | // 5 | // Created by Jon Hoffman on 8/11/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol TextValidationProtocol { 12 | var regExFindMatchString: String {get} 13 | var validationMessage: String {get} 14 | } 15 | 16 | 17 | extension TextValidationProtocol { 18 | 19 | var regExMatchingString: String { get { 20 | return regExFindMatchString + "$" 21 | } 22 | } 23 | 24 | func validateString(_ str: String) -> Bool { 25 | if let _ = str.range(of: regExMatchingString, options: .regularExpression) { 26 | return true 27 | } else { 28 | return false 29 | } 30 | } 31 | func getMatchingString(_ str: String) -> String? { 32 | if let newMatch = str.range(of: regExFindMatchString, options: .regularExpression) { 33 | return str.substring(with: newMatch) 34 | } else { 35 | return nil 36 | } 37 | } 38 | } 39 | 40 | 41 | class AlphaValidation: TextValidationProtocol { 42 | static let sharedInstance = AlphaValidation() 43 | fileprivate init(){} 44 | 45 | let regExFindMatchString = "^[a-zA-Z]{0,10}" 46 | let validationMessage = "Can only contain Alpha characters" 47 | } 48 | 49 | class AlphaNumericValidation: TextValidationProtocol { 50 | static let sharedInstance = AlphaNumericValidation() 51 | fileprivate init(){} 52 | 53 | let regExFindMatchString = "^[a-zA-Z0-9]{0,15}" 54 | let validationMessage = "Can only contain Alpha Numeric characters" 55 | } 56 | 57 | 58 | class DisplayNameValidation: TextValidationProtocol { 59 | static let sharedInstance = DisplayNameValidation() 60 | fileprivate init(){} 61 | 62 | let regExFindMatchString = "^[\\s?[a-zA-Z0-9\\-_\\s]]{0,15}" 63 | let validationMessage = "Display Name can contain only contain Alphanumeric Characters" 64 | } 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidation/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // TextValidation 4 | // 5 | // Created by Jon Hoffman on 8/11/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | @IBOutlet var alphaTextField: UITextField? 14 | @IBOutlet var alphaNumericTextField: UITextField? 15 | @IBOutlet var displayNameTextField: UITextField? 16 | @IBOutlet var messageLabel: UILabel? 17 | 18 | var validators = [UITextField: TextValidationProtocol]() 19 | 20 | override func viewDidLoad() { 21 | super.viewDidLoad() 22 | // Do any additional setup after loading the view, typically from a nib. 23 | if let alphaTextField = alphaTextField, let alphaNumericTextField = alphaNumericTextField, let displayNameTextField = displayNameTextField { 24 | 25 | alphaTextField.addTarget(self, action: #selector(ViewController.keyPressed(_:)), for: UIControlEvents.editingChanged) 26 | alphaNumericTextField.addTarget(self, action: #selector(ViewController.keyPressed(_:)), for: UIControlEvents.editingChanged) 27 | displayNameTextField.addTarget(self, action: #selector(ViewController.keyPressed(_:)), for: UIControlEvents.editingChanged) 28 | 29 | validators[alphaTextField] = AlphaValidation.sharedInstance 30 | validators[alphaNumericTextField] = AlphaNumericValidation.sharedInstance 31 | validators[displayNameTextField] = DisplayNameValidation.sharedInstance 32 | } 33 | 34 | 35 | } 36 | 37 | override func didReceiveMemoryWarning() { 38 | super.didReceiveMemoryWarning() 39 | // Dispose of any resources that can be recreated. 40 | } 41 | 42 | @IBAction func keyPressed(_ textField: UITextField) { 43 | if let validator = validators[textField] , !validator.validateString(textField.text!) { 44 | textField.text = validator.getMatchingString(textField.text!) 45 | messageLabel?.text = validator.validationMessage 46 | } 47 | } 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidationTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidationTests/TextValidationTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TextValidationTests.swift 3 | // TextValidationTests 4 | // 5 | // Created by Jon Hoffman on 8/11/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import TextValidation 11 | 12 | class TextValidationTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | func testExample() { 25 | // This is an example of a functional test case. 26 | // Use XCTAssert and related functions to verify your tests produce the correct results. 27 | } 28 | 29 | func testPerformanceExample() { 30 | // This is an example of a performance test case. 31 | self.measure { 32 | // Put the code you want to measure the time of here. 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidationUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter 05/TextValidation/TextValidationUITests/TextValidationUITests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TextValidationUITests.swift 3 | // TextValidationUITests 4 | // 5 | // Created by Jon Hoffman on 8/11/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | 11 | class TextValidationUITests: XCTestCase { 12 | 13 | override func setUp() { 14 | super.setUp() 15 | 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | 18 | // In UI tests it is usually best to stop immediately when a failure occurs. 19 | continueAfterFailure = false 20 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 21 | XCUIApplication().launch() 22 | } 23 | 24 | override func tearDown() { 25 | // Put teardown code here. This method is called after the invocation of each test method in the class. 26 | super.tearDown() 27 | } 28 | 29 | func testExample() { 30 | // Use recording to get started writing UI tests. 31 | // Use XCTAssert and related functions to verify your tests produce the correct results. 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter 06/AssociatedType.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | protocol MyProtocol { 4 | associatedtype E 5 | 6 | var items: [E] {get set} 7 | mutating func add(item: E) 8 | } 9 | 10 | struct MyIntType: MyProtocol { 11 | 12 | var items: [Int] = [] 13 | mutating func add(item: Int) { 14 | items.append(item) 15 | } 16 | } 17 | 18 | struct MyGenericType: MyProtocol { 19 | 20 | var items: [T] = [] 21 | mutating func add(item: T) { 22 | items.append(item) 23 | } 24 | } -------------------------------------------------------------------------------- /Chapter 06/AssociatedType.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 06/AssociatedType.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 06/AssociatedType.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 06/AssociatedType.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 06/ListProtocol.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | 3 | protocol List { 4 | associatedtype T 5 | mutating func add(item: T) 6 | func length() -> Int 7 | func get(index: Int) -> T? 8 | } 9 | 10 | struct ArrayList: List { 11 | var items: [T] 12 | 13 | mutating func add(item: T) { 14 | items.append(item) 15 | } 16 | func length() -> Int { 17 | return items.count 18 | } 19 | func get(index: Int) -> T? { 20 | return items[index] 21 | } 22 | } 23 | 24 | class LinkedList: List { 25 | var firstItem: T? 26 | 27 | func add(item: T) { 28 | //Add Item Logic 29 | } 30 | func length() -> Int { 31 | //return count 32 | return 0 33 | } 34 | func get(index: Int) -> T? { 35 | //Get Item at Index Logic 36 | return nil 37 | } 38 | } 39 | 40 | struct IntList: List { 41 | 42 | func add(item: Int) { 43 | //Add Item Logic 44 | } 45 | func length() -> Int { 46 | //return count 47 | return 0 48 | } 49 | func get(index: Int) -> Int? { 50 | //Get Item at Index Logic 51 | return nil 52 | } 53 | } -------------------------------------------------------------------------------- /Chapter 06/ListProtocol.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 06/ListProtocol.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 06/ListProtocol.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 06/ListProtocol.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/BridgePattern.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 07/BridgePattern.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 07/BridgePattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/BridgePattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/BridgePattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/BridgePattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/BridgePattern.playground/section-1.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | protocol MessageProtocol { 3 | var messageString: String {get set} 4 | init(messageString: String) 5 | func prepareMessage() 6 | } 7 | 8 | protocol SenderProtocol { 9 | var message: MessageProtocol? {get set} 10 | func sendMessage() 11 | func verifyMessage() 12 | } 13 | 14 | class PlainTextMessage: MessageProtocol { 15 | var messageString: String 16 | required init(messageString: String) { 17 | self.messageString = messageString 18 | } 19 | func prepareMessage() { 20 | // Nothing to do 21 | } 22 | } 23 | 24 | class AESEncryptedMessage: MessageProtocol { 25 | var messageString: String 26 | required init(messageString: String) { 27 | self.messageString = messageString 28 | } 29 | func prepareMessage() { 30 | self.messageString = "AES: " + self.messageString 31 | } 32 | } 33 | 34 | 35 | class DESEncryptedMessage: MessageProtocol { 36 | var messageString: String 37 | required init(messageString: String) { 38 | self.messageString = messageString 39 | } 40 | func prepareMessage() { 41 | self.messageString = "DES: " + self.messageString 42 | } 43 | } 44 | 45 | 46 | class EmailSender: SenderProtocol { 47 | var message: MessageProtocol? 48 | func sendMessage() { 49 | print("Sending through E-Mail:") 50 | print(" \(message!.messageString)") 51 | } 52 | func verifyMessage() { 53 | print("Verifying E-Mail message") 54 | } 55 | } 56 | 57 | class SMSSender: SenderProtocol { 58 | var message: MessageProtocol? 59 | func sendMessage() { 60 | print("Sending through SMS:") 61 | print(" \(message!.messageString)") 62 | } 63 | func verifyMessage() { 64 | print("Verifying SMS message") 65 | } 66 | } 67 | 68 | struct MessageingBridge { 69 | static func sendMessage(message: MessageProtocol, sender: SenderProtocol) { 70 | var sender = sender 71 | message.prepareMessage() 72 | sender.message = message 73 | sender.verifyMessage() 74 | sender.sendMessage() 75 | } 76 | 77 | } 78 | 79 | 80 | var myMessage = PlainTextMessage(messageString: "Plain Text Message") 81 | var myEncMessage = AESEncryptedMessage(messageString: "Encrypted Message") 82 | 83 | MessageingBridge.sendMessage(message: myMessage, sender: SMSSender()) 84 | 85 | MessageingBridge.sendMessage(message: myMessage, sender: EmailSender()) 86 | 87 | MessageingBridge.sendMessage(message: myEncMessage, sender: EmailSender()) 88 | -------------------------------------------------------------------------------- /Chapter 07/Builder Pattern.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 07/Builder Pattern.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 07/Builder Pattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/Builder Pattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/Builder Pattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/Builder Pattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/CommandPattern.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 07/CommandPattern.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 07/CommandPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/CommandPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/CommandPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/CommandPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/CommandPattern.playground/section-1.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | protocol Command { 4 | func execute() 5 | } 6 | 7 | struct RockerSwitchLightOnCommand: Command { 8 | func execute() { 9 | print("Rocker Switch: Turning Light On") 10 | } 11 | } 12 | 13 | struct RockerSwitchLightOffCommand: Command { 14 | func execute() { 15 | print("ocker Switch: Turning Light Off") 16 | } 17 | } 18 | 19 | struct PullSwitchLightOnCommand: Command { 20 | func execute() { 21 | print("Pull Switch: Turning Light On") 22 | } 23 | } 24 | 25 | struct PullSwitchLightOffCommand: Command { 26 | func execute() { 27 | print("Pull Switch: Turning Light Off") 28 | } 29 | } 30 | 31 | class Light { 32 | private var lightOnCommand: Command 33 | private var lightOffCommand: Command 34 | 35 | init(lightOnCommand: Command, lightOffCommand: Command) { 36 | self.lightOnCommand = lightOnCommand 37 | self.lightOffCommand = lightOffCommand 38 | } 39 | 40 | func turnOnLight() { 41 | self.lightOnCommand.execute() 42 | } 43 | 44 | func turnOffLight() { 45 | self.lightOffCommand.execute() 46 | } 47 | } 48 | 49 | 50 | var on = PullSwitchLightOnCommand() 51 | var off = PullSwitchLightOffCommand() 52 | var light = Light(lightOnCommand: on, lightOffCommand: off) 53 | 54 | light.turnOnLight() 55 | light.turnOffLight() 56 | -------------------------------------------------------------------------------- /Chapter 07/CommandPattern.playground/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 07/FacadePattern.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 07/FacadePattern.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 07/FacadePattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/FacadePattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/FacadePattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/FacadePattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/FacadePattern.playground/section-1.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | struct Hotel { 4 | //Information about hotel room 5 | } 6 | 7 | struct HotelBooking { 8 | static func getHotelNameForDates(to: NSDate, from: NSDate) -> [Hotel]? { 9 | let hotels = [Hotel]() 10 | //logic to get hotels 11 | return hotels 12 | } 13 | 14 | static func bookHotel(hotel: Hotel) { 15 | // logic to reserve hotel room 16 | } 17 | } 18 | 19 | 20 | struct Flight { 21 | //Information about flights 22 | } 23 | 24 | struct FlightBooking { 25 | static func getFlightNameForDates(to: NSDate, from: NSDate) -> [Flight]? { 26 | let flights = [Flight]() 27 | //logic to get flights 28 | return flights 29 | } 30 | 31 | static func bookFlight(fight: Flight) { 32 | // logic to reserve flight 33 | } 34 | } 35 | 36 | 37 | struct RentalCar { 38 | //Information about rental cars 39 | } 40 | 41 | struct RentalCarBooking { 42 | static func getRentalCarNameForDates(to: NSDate, from: NSDate) -> [RentalCar]? { 43 | let cars = [RentalCar]() 44 | //logic to get flights 45 | return cars 46 | } 47 | 48 | static func bookRentalCar(rentalCar: RentalCar) { 49 | // logic to reserve rental car 50 | } 51 | } 52 | 53 | 54 | class TravelFacade { 55 | 56 | var hotels: [Hotel]? 57 | var flights: [Flight]? 58 | var cars: [RentalCar]? 59 | 60 | init(to: NSDate, from: NSDate) { 61 | hotels = HotelBooking.getHotelNameForDates(to: to, from: from) 62 | flights = FlightBooking.getFlightNameForDates(to: to, from: from) 63 | cars = RentalCarBooking.getRentalCarNameForDates(to: to, from: from) 64 | } 65 | 66 | func bookTrip(hotel: Hotel, flight: Flight, rentalCar: RentalCar) { 67 | HotelBooking.bookHotel(hotel: hotel) 68 | FlightBooking.bookFlight(fight: flight) 69 | RentalCarBooking.bookRentalCar(rentalCar: rentalCar) 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /Chapter 07/FactoryMethodPattern.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 07/FactoryMethodPattern.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 07/FactoryMethodPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/FactoryMethodPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/FactoryMethodPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/FactoryMethodPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/FactoryMethodPattern.playground/section-1.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | protocol TextValidationProtocol { 4 | var regExFindMatchString: String {get} 5 | var validationMessage: String {get} 6 | 7 | } 8 | 9 | 10 | extension TextValidationProtocol { 11 | 12 | var regExMatchingString: String { get { 13 | return regExFindMatchString + "$" 14 | } 15 | } 16 | 17 | func validateString(str: String) -> Bool { 18 | if let _ = str.range(of: regExMatchingString, options: .regularExpression) { 19 | return true 20 | } else { 21 | return false 22 | } 23 | } 24 | func getMatchingString(str: String) -> String? { 25 | if let newMatch = str.range(of: regExFindMatchString, options: .regularExpression) { 26 | return str.substring(with: newMatch) 27 | } else { 28 | return nil 29 | } 30 | } 31 | } 32 | 33 | 34 | class AlphaValidation: TextValidationProtocol { 35 | static let sharedInstance = AlphaValidation() 36 | private init(){} 37 | 38 | let regExFindMatchString = "^[a-zA-Z]{0,10}" 39 | let validationMessage = "Can only contain Alpha characters" 40 | } 41 | 42 | class AlphaNumericValidation: TextValidationProtocol { 43 | static let sharedInstance = AlphaNumericValidation() 44 | private init(){} 45 | 46 | let regExFindMatchString = "^[a-zA-Z0-9]{0,10}" 47 | let validationMessage = "Can only contain Alpha Numeric characters" 48 | } 49 | 50 | class NumericValidation: TextValidationProtocol { 51 | static let sharedInstance = NumericValidation() 52 | private init(){} 53 | 54 | let regExFindMatchString = "^[0-9]{0,10}" 55 | let validationMessage = "Display Name can contain a maximum of 15 Alphanumeric Characters" 56 | } 57 | 58 | 59 | func getValidator(alphaCharacters: Bool, numericCharacters: Bool) -> TextValidationProtocol? { 60 | if alphaCharacters && numericCharacters { 61 | return AlphaNumericValidation.sharedInstance 62 | } else if alphaCharacters && !numericCharacters { 63 | return AlphaValidation.sharedInstance 64 | } else if !alphaCharacters && numericCharacters { 65 | return NumericValidation.sharedInstance 66 | } else { 67 | return nil 68 | } 69 | } 70 | 71 | var str = "abc123" 72 | var validator1 = getValidator(alphaCharacters: true, numericCharacters: false) 73 | print("String validated: \(validator1?.validateString(str: str))") 74 | 75 | var validator2 = getValidator(alphaCharacters: true, numericCharacters: true) 76 | print("String validated: \(validator2?.validateString(str: str))") 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /Chapter 07/ObserverPattern.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | 4 | //Notification Center Example 5 | let NCNAME = "Notification Name" 6 | 7 | class PostType { 8 | let nc = NotificationCenter.default 9 | 10 | func post() { 11 | nc.post(name: Notification.Name(rawValue: NCNAME), object: nil) 12 | } 13 | } 14 | 15 | class ObserverType { 16 | let nc = NotificationCenter.default 17 | 18 | init() { 19 | nc.addObserver(self, selector: #selector(receiveNotification(notification:)), name: Notification.Name(rawValue: NCNAME), object: nil) 20 | } 21 | 22 | @objc func receiveNotification(notification: Notification) { 23 | print("Notification Received") 24 | } 25 | } 26 | 27 | var postType = PostType() 28 | var observerType = ObserverType() 29 | postType.post() 30 | 31 | 32 | //Observer type Example 33 | protocol ZombieObserverProtocol { 34 | func turnLeft() 35 | func turnRight() 36 | func seesUs() 37 | } 38 | 39 | class MyObserver: ZombieObserverProtocol { 40 | func turnLeft() { 41 | print("Zombie turned left, we move right") 42 | } 43 | func turnRight() { 44 | print("Zombie turned right, we move left") 45 | } 46 | func seesUs() { 47 | print("Zombie sees us, RUN!!!!") 48 | } 49 | } 50 | 51 | struct Zombie { 52 | var observer: ZombieObserverProtocol 53 | 54 | func turnZombieLeft() { 55 | //Zombie turns left 56 | //Notify observer 57 | observer.turnLeft() 58 | } 59 | func turnZombieRight() { 60 | //Zombie turns right 61 | //Notify observer 62 | observer.turnRight() 63 | } 64 | func spotHuman() { 65 | //Zombie sees us 66 | //Notify observer 67 | observer.seesUs() 68 | } 69 | } 70 | 71 | var observer = MyObserver() 72 | var zombie = Zombie(observer: observer) 73 | 74 | zombie.turnZombieLeft() 75 | zombie.spotHuman() 76 | 77 | 78 | //Property Observer Example 79 | protocol PropertyObserverProtocol { 80 | func propertyChanged(propertyName: String, newValue: Any) 81 | } 82 | 83 | class MyObserverType: PropertyObserverProtocol { 84 | func propertyChanged(propertyName: String, newValue: Any) { 85 | print("----changed----") 86 | print("Property Name: \(propertyName)") 87 | print("New Value: \(newValue)") 88 | } 89 | } 90 | 91 | struct PropertyObserver { 92 | var observer: PropertyObserverProtocol 93 | var property1: String { 94 | didSet{ 95 | observer.propertyChanged(propertyName: "property1", newValue: property1) 96 | } 97 | willSet(newValue) { 98 | print("Property Changing") 99 | } 100 | } 101 | } 102 | 103 | var myObserver = MyObserverType() 104 | var p = PropertyObserver(observer: myObserver, property1: "Initial String") 105 | p.property1 = "My String" 106 | -------------------------------------------------------------------------------- /Chapter 07/ObserverPattern.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 07/ObserverPattern.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 07/ObserverPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/ObserverPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/ObserverPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/ObserverPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/ProxyPattern.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 07/ProxyPattern.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 07/ProxyPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/ProxyPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/ProxyPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/ProxyPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/ProxyPattern.playground/section-1.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | protocol FloorPlanProtocol { 4 | var bedRooms: Int {get set} 5 | var utilityRooms: Int {get set} 6 | var bathRooms: Int {get set} 7 | var kitchen: Int {get set} 8 | var livingRooms: Int {get set} 9 | } 10 | 11 | 12 | struct FloorPlan: FloorPlanProtocol { 13 | var bedRooms = 0 14 | var utilityRooms = 0 15 | var bathRooms = 0 16 | var kitchen = 0 17 | var livingRooms = 0 18 | } 19 | 20 | class House { 21 | var stories = [FloorPlanProtocol]() 22 | 23 | func addStory(floorPlan: FloorPlanProtocol) { 24 | stories.append(floorPlan) 25 | } 26 | } 27 | 28 | class HouseProxy { 29 | var house = House() 30 | 31 | func addStory(floorPlan: FloorPlanProtocol) -> Bool { 32 | if house.stories.count < 3 { 33 | house.addStory(floorPlan: floorPlan) 34 | return true 35 | } else { 36 | return false 37 | } 38 | } 39 | } 40 | 41 | 42 | var ourHouse = HouseProxy() 43 | var basement = FloorPlan(bedRooms: 0, utilityRooms: 1, bathRooms: 1, kitchen: 0, livingRooms: 1) 44 | var firstStory = FloorPlan(bedRooms: 1, utilityRooms: 0, bathRooms: 2, kitchen: 1, livingRooms: 1) 45 | var secondStory = FloorPlan(bedRooms: 2, utilityRooms: 0, bathRooms: 1, kitchen: 0, livingRooms: 1) 46 | var additionalStory = FloorPlan(bedRooms: 1, utilityRooms: 0, bathRooms: 1, kitchen: 1, livingRooms: 1) 47 | 48 | print(ourHouse.addStory(floorPlan: basement)) 49 | print(ourHouse.addStory(floorPlan: firstStory)) 50 | print(ourHouse.addStory(floorPlan: secondStory)) 51 | print(ourHouse.addStory(floorPlan: additionalStory)) 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Chapter 07/SingletonPattern.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 07/SingletonPattern.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 07/SingletonPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/SingletonPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/SingletonPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/SingletonPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/SingletonPattern.playground/section-1.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | class MySingleton { 4 | static let sharedInstance = MySingleton() 5 | var number = 0 6 | 7 | private init() {} 8 | 9 | } 10 | 11 | 12 | var singleA = MySingleton.sharedInstance 13 | singleA.number = 1 14 | 15 | var singleB = MySingleton.sharedInstance 16 | singleB.number = 2 17 | 18 | print(singleA.number) 19 | print(singleB.number) 20 | 21 | var singleC = MySingleton.sharedInstance 22 | singleB.number = 3 23 | 24 | print(singleA.number) 25 | print(singleB.number) 26 | print(singleC.number) 27 | -------------------------------------------------------------------------------- /Chapter 07/StrategyPattern.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Chapter 07/StrategyPattern.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 07/StrategyPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/StrategyPattern.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/StrategyPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 07/StrategyPattern.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 07/StrategyPattern.playground/section-1.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | protocol CompressionStrategy { 4 | func compressFiles(filePaths: [String]) 5 | } 6 | 7 | struct ZipCompressionStrategy: CompressionStrategy { 8 | func compressFiles(filePaths: [String]) { 9 | print("Using Zip Compression") 10 | } 11 | } 12 | struct RarCompressionStrategy: CompressionStrategy { 13 | func compressFiles(filePaths: [String]) { 14 | print("Using RAR Compression") 15 | } 16 | } 17 | 18 | class CompressContent { 19 | var strategy: CompressionStrategy 20 | 21 | init(strategy: CompressionStrategy) { 22 | self.strategy = strategy 23 | } 24 | 25 | func compressFiles(filePaths: [String]) { 26 | self.strategy.compressFiles(filePaths: filePaths) 27 | } 28 | } 29 | 30 | var filePaths = ["file1.txt", "file2.txt"] 31 | var zip = ZipCompressionStrategy() 32 | var rar = RarCompressionStrategy() 33 | 34 | var compress = CompressContent(strategy: zip) 35 | compress.compressFiles(filePaths: filePaths) 36 | 37 | compress.strategy = rar 38 | compress.compressFiles(filePaths: filePaths) 39 | 40 | 41 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator.xcodeproj/project.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 08/Calculator/Calculator/Calculator.xcodeproj/project.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator.xcodeproj/project.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 08/Calculator/Calculator/Calculator.xcodeproj/project.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator.xcodeproj/xcuserdata/hoffmanjon.xcuserdatad/xcschemes/Calculator.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 95 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator.xcodeproj/xcuserdata/hoffmanjon.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Calculator.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 34DA26FA1C2C6384003C2D6C 16 | 17 | primary 18 | 19 | 20 | 34DA270E1C2C6384003C2D6C 21 | 22 | primary 23 | 24 | 25 | 34DA27191C2C6384003C2D6C 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator.xcodeproj/xcuserdata/jonhoffman.xcuserdatad/xcschemes/Calculator.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 95 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator.xcodeproj/xcuserdata/jonhoffman.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Calculator.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 34DA26FA1C2C6384003C2D6C 16 | 17 | primary 18 | 19 | 20 | 34DA270E1C2C6384003C2D6C 21 | 22 | primary 23 | 24 | 25 | 34DA27191C2C6384003C2D6C 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Calculator 4 | // 5 | // Created by Jon Hoffman on 12/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator/Calculator.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Calculator.swift 3 | // Calculator 4 | // 5 | // Created by Jon Hoffman on 12/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | struct Calculator { 12 | var currentValue: Double = 0.0 13 | var lastCommand: CommandProtocol? 14 | 15 | mutating func clear() { 16 | currentValue = 0.0 17 | } 18 | 19 | mutating func commandEntered(_ newNumber: Double, nextCommand: CommandProtocol?) { 20 | if let lastCommand = self.lastCommand { 21 | self.currentValue = lastCommand.execute(currentValue, secondValue: newNumber) 22 | } else { 23 | self.currentValue = newNumber 24 | } 25 | lastCommand = nextCommand 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator/CalculatorCommands.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CalculatorCommands.swift 3 | // Calculator 4 | // 5 | // Created by Jon Hoffman on 12/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | protocol CommandProtocol { 12 | func execute(_ firstValue: Double, secondValue:Double) -> Double 13 | } 14 | 15 | struct AddCommand: CommandProtocol { 16 | func execute(_ firstValue: Double, secondValue: Double) -> Double { 17 | return firstValue + secondValue 18 | } 19 | } 20 | 21 | struct SubCommand: CommandProtocol { 22 | func execute(_ firstValue: Double, secondValue: Double) -> Double { 23 | return firstValue - secondValue 24 | } 25 | } 26 | 27 | struct MultiplyCommand: CommandProtocol { 28 | func execute(_ firstValue: Double, secondValue: Double) -> Double { 29 | return firstValue * secondValue 30 | } 31 | } 32 | 33 | struct DivideCommand: CommandProtocol { 34 | func execute(_ firstValue: Double, secondValue: Double) -> Double { 35 | return firstValue / secondValue 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/Calculator/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // Calculator 4 | // 5 | // Created by Jon Hoffman on 12/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController { 12 | 13 | @IBOutlet var display: UILabel! 14 | var calculator = Calculator() 15 | 16 | override func viewDidLoad() { 17 | super.viewDidLoad() 18 | // Do any additional setup after loading the view, typically from a nib. 19 | } 20 | 21 | override func didReceiveMemoryWarning() { 22 | super.didReceiveMemoryWarning() 23 | // Dispose of any resources that can be recreated. 24 | } 25 | 26 | 27 | @IBAction func commandEntered(_ sender: UIButton) { 28 | if let text = display.text, let num = Double(text) { 29 | var clearDisplay = true 30 | switch sender.tag { 31 | case 0: 32 | calculator.commandEntered(num, nextCommand: nil) 33 | display.text = "\(calculator.currentValue)" 34 | clearDisplay = false 35 | case 1: 36 | calculator.commandEntered(num, nextCommand: AddCommand()) 37 | case 2: 38 | calculator.commandEntered(num, nextCommand: SubCommand()) 39 | case 3: 40 | calculator.commandEntered(num, nextCommand: MultiplyCommand()) 41 | case 4: 42 | calculator.commandEntered(num, nextCommand: DivideCommand()) 43 | case 5: 44 | calculator.clear() 45 | case 6: 46 | if let text = display.text { 47 | let newText = text.substring(to: text.characters.index(before: text.endIndex)) 48 | display.text = newText 49 | } 50 | clearDisplay = false 51 | default: 52 | break 53 | } 54 | if clearDisplay { 55 | display.text = "0" 56 | } 57 | } 58 | } 59 | 60 | @IBAction func numberEntered(_ sender: UIButton) { 61 | if let text = display.text { 62 | let tag = sender.tag 63 | if tag < 10 { 64 | display.text = "\(text)\(tag)" 65 | } else { 66 | display.text = "\(text)." 67 | } 68 | } 69 | 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/CalculatorTests/CalculatorTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CalculatorTests.swift 3 | // CalculatorTests 4 | // 5 | // Created by Jon Hoffman on 12/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import Calculator 11 | 12 | class CalculatorTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | func testExample() { 25 | // This is an example of a functional test case. 26 | // Use XCTAssert and related functions to verify your tests produce the correct results. 27 | } 28 | 29 | func testPerformanceExample() { 30 | // This is an example of a performance test case. 31 | self.measure { 32 | // Put the code you want to measure the time of here. 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/CalculatorTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/CalculatorUITests/CalculatorUITests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // CalculatorUITests.swift 3 | // CalculatorUITests 4 | // 5 | // Created by Jon Hoffman on 12/24/15. 6 | // Copyright © 2015 Jon Hoffman. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | 11 | class CalculatorUITests: XCTestCase { 12 | 13 | override func setUp() { 14 | super.setUp() 15 | 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | 18 | // In UI tests it is usually best to stop immediately when a failure occurs. 19 | continueAfterFailure = false 20 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 21 | XCUIApplication().launch() 22 | 23 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 24 | } 25 | 26 | override func tearDown() { 27 | // Put teardown code here. This method is called after the invocation of each test method in the class. 28 | super.tearDown() 29 | } 30 | 31 | func testExample() { 32 | // Use recording to get started writing UI tests. 33 | // Use XCTAssert and related functions to verify your tests produce the correct results. 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Chapter 08/Calculator/Calculator/CalculatorUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /Chapter 08/DataAccessCaseStudy.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 08/DataAccessCaseStudy.zip -------------------------------------------------------------------------------- /Chapter 08/Logger.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter 08/Logger.playground/playground.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter 08/Logger.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 08/Logger.playground/playground.xcworkspace/xcuserdata/hoffmanjon.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Chapter 08/Logger.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Swift-3-Protocol-Oriented-Programming-Second-Edition/c925d0e348dac301ae705c41fea09c857a7a9bd6/Chapter 08/Logger.playground/playground.xcworkspace/xcuserdata/jonhoffman.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /License: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #Swift 3 Protocol-Oriented Programming - Second Edition 2 | This is the code repository for [Swift 3 Protocol-Oriented Programming - Second Edition](https://www.packtpub.com/application-development/swift-3-protocol-oriented-programming-second-edition?utm_source=github&utm_medium=repository&utm_campaign=9781787129948), published by [Packt](www.packtpub.com). It contains all the supporting project files necessary to work through the book from start to finish. 3 | ## About the Book 4 | One of the most important additions to the new features and capabilities of the Swift programming language was an overhaul of Protocols. Protocol-oriented programming and first class value semantics have now become two incredibly powerful concepts at the heart of Swift’s design. 5 | ##Instructions and Navigations 6 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02. 7 | 8 | 9 | 10 | The code will look like the following: 11 | ``` 12 | 13 | enum TerrainType { 14 | case Land 15 | case Sea 16 | case Air 17 | } 18 | ``` 19 | 20 | To follow along with the examples in this book, the reader will need to have an Apple 21 | computer with OS X 10.12 or higher installed. They will also need to install Xcode version 22 | 8.0 or higher with Swift version 3 or higher. At least a basic knowledge of the Swift 23 | programming language is required. 24 | 25 | ##Related Products 26 | * [Protocol-Oriented Programming with Swift](https://www.packtpub.com/application-development/protocol-oriented-programming-swift?utm_source=github&utm_medium=repository&utm_campaign=9781785882944) 27 | 28 | * [Swift: Developing iOS Applications](https://www.packtpub.com/virtualization-and-cloud/swift-developing-ios-applications?utm_source=github&utm_medium=repository&utm_campaign=9781787120242) 29 | 30 | * [Swift 3 Programming Cookbook](https://www.packtpub.com/application-development/swift-3-programming-cookbook?utm_source=github&utm_medium=repository&utm_campaign=9781786460899) 31 | ###Suggestions and Feedback 32 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSe5qwunkGf6PUvzPirPDtuy1Du5Rlzew23UBp2S-P3wB-GcwQ/viewform) if you have any feedback or suggestions. 33 | --------------------------------------------------------------------------------