├── Kickstart.jpg ├── 8Protocols.playground ├── Pages │ ├── Self.xcplaygroundpage │ │ ├── Sources │ │ │ ├── Movable.swift │ │ │ ├── Size.swift │ │ │ ├── Vertex.swift │ │ │ └── Rectangle.swift │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Casting.xcplaygroundpage │ │ ├── Sources │ │ │ ├── Movable.swift │ │ │ ├── Size.swift │ │ │ ├── Vertex.swift │ │ │ └── Rectangle.swift │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Creating a Protocol.xcplaygroundpage │ │ ├── Sources │ │ │ ├── Movable.swift │ │ │ ├── Size.swift │ │ │ ├── Vertex.swift │ │ │ └── Rectangle.swift │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Conforming to a Protocol.xcplaygroundpage │ │ ├── Sources │ │ │ ├── Movable.swift │ │ │ ├── Size.swift │ │ │ ├── Vertex.swift │ │ │ └── Rectangle.swift │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Generics.xcplaygroundpage │ │ ├── Sources │ │ │ ├── Movable.swift │ │ │ ├── Size.swift │ │ │ ├── Vertex.swift │ │ │ └── Rectangle.swift │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Properties.xcplaygroundpage │ │ ├── Sources │ │ │ ├── Movable.swift │ │ │ ├── Size.swift │ │ │ ├── Vertex.swift │ │ │ └── Rectangle.swift │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Polymorphism.xcplaygroundpage │ │ ├── Sources │ │ │ ├── Movable.swift │ │ │ ├── Size.swift │ │ │ ├── Vertex.swift │ │ │ └── Rectangle.swift │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── TOC.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Custom Operators.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ ├── Sources │ │ │ ├── Size.swift │ │ │ ├── Movable.swift │ │ │ ├── Vertex.swift │ │ │ └── Rectangle.swift │ │ └── Contents.swift │ └── Protocol Extensions.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ ├── Sources │ │ ├── Size.swift │ │ ├── Movable.swift │ │ ├── Vertex.swift │ │ └── Rectangle.swift │ │ └── Contents.swift ├── xcuserdata │ └── dimsum.xcuserdatad │ │ └── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist ├── playground.xcworkspace │ └── xcuserdata │ │ └── dimsum.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── contents.xcplayground ├── 1BeforeWeBegin.playground ├── Contents.swift ├── Pages │ ├── TOC.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── Main Page.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ └── Second Page.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift ├── timeline.xctimeline ├── playground.xcworkspace │ └── xcuserdata │ │ └── dimsum.xcuserdatad │ │ ├── UserInterfaceState.xcuserstate │ │ └── WorkspaceSettings.xcsettings └── contents.xcplayground ├── 2Functions.playground ├── Pages │ ├── TOC.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Default Values.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── External Names.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Return Values.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Multiple Parameters.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Returning Tuples.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Single Parameter.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Variadic Parameters.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── No Parameter, No Return.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── The print() Function.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ └── Overloading.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline ├── playground.xcworkspace │ └── xcuserdata │ │ └── dimsum.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── contents.xcplayground ├── 7Structs.playground ├── Pages │ ├── TOC.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── By Value.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Composition.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ ├── Contents.swift │ │ └── Sources │ │ │ ├── Size.swift │ │ │ ├── Vertex.swift │ │ │ └── Rectangle.swift │ ├── Equatable.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Methods.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Access Levels.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ ├── Contents.swift │ │ └── Sources │ │ │ └── Vertex.swift │ ├── Will Set Did Set.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Computed Properties.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Creating a Struct.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Stored Properties.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ └── Custom String Convertible.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift ├── playground.xcworkspace │ └── xcuserdata │ │ └── dimsum.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── contents.xcplayground ├── 9Classes.playground ├── Pages │ ├── TOC.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Composition.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Methods.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Subclasses.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Creating a Class.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Initializers.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Failable Initializers.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Loading and Unloading.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Optional Chaining.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Required Initializers.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Composition Preparation.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ └── Optional Property Values.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift ├── playground.xcworkspace │ └── xcuserdata │ │ └── dimsum.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── contents.xcplayground ├── 4Collections.playground ├── Pages │ ├── Sets.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── TOC.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── One Type.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Enumerate Arrays.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Modify Arrays.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Create Dictionaries.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Enumerate Dictionaries.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Modify Dictionaries.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Optionals and Dictionaries.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Value and Reference Types.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ └── Create Arrays.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift ├── playground.xcworkspace │ └── xcuserdata │ │ └── dimsum.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── contents.xcplayground ├── 6Enumerations.playground ├── Pages │ ├── TOC.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── By Value.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Error Type.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Option Sets.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Associated Values.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Computed Properties.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Numerical Raw Values.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── String Raw Values.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── The Switch Statement.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Creating an Enumeration.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Binding to Associated Values.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ └── Methods.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift ├── playground.xcworkspace │ └── xcuserdata │ │ └── dimsum.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── contents.xcplayground ├── 5FlexibleFunctions.playground ├── Pages │ ├── TOC.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Extensions.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Inout Parameters.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Reference Types.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Return Values.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Function Parameters.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Transforming Arrays.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Understanding Map.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Higher Order Functions.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Generics.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ └── Closure Expressions.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline ├── playground.xcworkspace │ └── xcuserdata │ │ └── dimsum.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── contents.xcplayground ├── 3VariablesAndConstants.playground ├── Pages │ ├── Let.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── TOC.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Types.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Type Alias.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Inferring Type.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Nil.xcplaygroundpage │ │ ├── Resources │ │ │ └── Kickstart.jpg │ │ ├── Contents.swift │ │ └── timeline.xctimeline │ ├── Numeric Types.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ ├── Higher Order Functions.xcplaygroundpage │ │ ├── timeline.xctimeline │ │ └── Contents.swift │ └── Var.xcplaygroundpage │ │ ├── Contents.swift │ │ └── timeline.xctimeline ├── playground.xcworkspace │ └── xcuserdata │ │ └── dimsum.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── contents.xcplayground ├── README.md └── .gitignore /Kickstart.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/editorscut/ec007-swift-kickstart/HEAD/Kickstart.jpg -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Self.xcplaygroundpage/Sources/Movable.swift: -------------------------------------------------------------------------------- 1 | public protocol Movable { 2 | func moveByX(deltaX: Double) -> Self 3 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Casting.xcplaygroundpage/Sources/Movable.swift: -------------------------------------------------------------------------------- 1 | public protocol Movable { 2 | func moveByX(deltaX: Double) -> Movable 3 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Creating a Protocol.xcplaygroundpage/Sources/Movable.swift: -------------------------------------------------------------------------------- 1 | public protocol Movable { 2 | func moveByX(deltaX: Double) -> Movable 3 | } -------------------------------------------------------------------------------- /1BeforeWeBegin.playground/Contents.swift: -------------------------------------------------------------------------------- 1 | //: Playground - noun: a place where people can play 2 | 3 | import UIKit 4 | var str = "Hello, playground" 5 | str 6 | 7 | 8 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Conforming to a Protocol.xcplaygroundpage/Sources/Movable.swift: -------------------------------------------------------------------------------- 1 | public protocol Movable { 2 | func moveByX(deltaX: Double) -> Movable 3 | } -------------------------------------------------------------------------------- /1BeforeWeBegin.playground/Pages/TOC.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | /*: 2 | 3 | # Table of Contents 4 | * [Main Page](Main%20Page) 5 | * [Second Page](Second%20Page) 6 | */ 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Generics.xcplaygroundpage/Sources/Movable.swift: -------------------------------------------------------------------------------- 1 | public protocol Movable { 2 | var location: Vertex {get} 3 | func moveByX(deltaX: Double) -> Self 4 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Properties.xcplaygroundpage/Sources/Movable.swift: -------------------------------------------------------------------------------- 1 | public protocol Movable { 2 | var location: Vertex {get} 3 | func moveByX(deltaX: Double) -> Self 4 | } -------------------------------------------------------------------------------- /1BeforeWeBegin.playground/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Polymorphism.xcplaygroundpage/Sources/Movable.swift: -------------------------------------------------------------------------------- 1 | public protocol Movable { 2 | var location: Vertex {get} 3 | func moveByX(deltaX: Double) -> Self 4 | } -------------------------------------------------------------------------------- /8Protocols.playground/xcuserdata/dimsum.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/TOC.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/TOC.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Self.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/TOC.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/TOC.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /1BeforeWeBegin.playground/Pages/TOC.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Sets.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/TOC.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/TOC.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/By Value.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Composition.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Equatable.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Methods.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Casting.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Generics.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Composition.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Methods.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Subclasses.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /1BeforeWeBegin.playground/Pages/Main Page.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Default Values.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/External Names.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Return Values.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/One Type.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/TOC.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/By Value.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Error Type.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Option Sets.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Access Levels.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Will Set Did Set.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Polymorphism.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Properties.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Creating a Class.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Initializers.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /1BeforeWeBegin.playground/Pages/Second Page.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Multiple Parameters.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Returning Tuples.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Single Parameter.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Variadic Parameters.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Let.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/TOC.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Types.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Enumerate Arrays.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Modify Arrays.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Extensions.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Computed Properties.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Creating a Struct.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Stored Properties.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Creating a Protocol.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Custom Operators.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Protocol Extensions.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Failable Initializers.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Loading and Unloading.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Optional Chaining.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Required Initializers.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/No Parameter, No Return.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/The print() Function.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Type Alias.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Create Dictionaries.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Enumerate Dictionaries.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Modify Dictionaries.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Inout Parameters.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Reference Types.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Return Values.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Associated Values.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Computed Properties.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Numerical Raw Values.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/String Raw Values.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/The Switch Statement.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Custom String Convertible.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Conforming to a Protocol.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Composition Preparation.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Optional Property Values.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Inferring Type.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Nil.xcplaygroundpage/Resources/Kickstart.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/editorscut/ec007-swift-kickstart/HEAD/3VariablesAndConstants.playground/Pages/Nil.xcplaygroundpage/Resources/Kickstart.jpg -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Numeric Types.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Optionals and Dictionaries.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Value and Reference Types.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Function Parameters.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Transforming Arrays.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Understanding Map.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Creating an Enumeration.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Higher Order Functions.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Higher Order Functions.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Binding to Associated Values.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Inferring Type.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Inferring Type 2 | //: [TOC](TOC) | Previous | [Next](@next) 3 | 4 | let person = "Swift Programmer" 5 | 6 | //: [TOC](TOC) | Previous | [Next](@next) 7 | -------------------------------------------------------------------------------- /1BeforeWeBegin.playground/Pages/Main Page.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Main Page 2 | //: [TOC](TOC) | Previous |[Next](@next) 3 | import UIKit 4 | var str = "Hello, playground" 5 | str 6 | //: [TOC](TOC) | Previous |[Next](@next) 7 | 8 | 9 | -------------------------------------------------------------------------------- /2Functions.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/editorscut/ec007-swift-kickstart/HEAD/2Functions.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /7Structs.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/editorscut/ec007-swift-kickstart/HEAD/7Structs.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /8Protocols.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/editorscut/ec007-swift-kickstart/HEAD/8Protocols.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /9Classes.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/editorscut/ec007-swift-kickstart/HEAD/9Classes.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ec007-swift-kickstart 2 | ===================== 3 | 4 | This is the sample code that accompanies the editorscut.com book "A Swift Kickstart" http://editorscut.com 5 | 6 | The code is for Swift 2.0 which requires Xcode 7 running on Yosemite or El Capitan. 7 | 8 | -------------------------------------------------------------------------------- /4Collections.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/editorscut/ec007-swift-kickstart/HEAD/4Collections.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Creating a Protocol.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Creating a Protocol 2 | //: [TOC](TOC) | Previous | [Next](@next) 3 | 4 | //: All of the content is in the Sources directory 5 | 6 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 7 | -------------------------------------------------------------------------------- /1BeforeWeBegin.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/editorscut/ec007-swift-kickstart/HEAD/1BeforeWeBegin.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /6Enumerations.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/editorscut/ec007-swift-kickstart/HEAD/6Enumerations.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /7Structs.playground/Pages/Creating a Struct.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Creating a Struct 2 | //: [TOC](TOC) | Previous | [Next](@next) 3 | 4 | struct Vertex { 5 | 6 | } 7 | 8 | let point = Vertex() 9 | 10 | //: [TOC](TOC) | Previous | [Next](@next) 11 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/editorscut/ec007-swift-kickstart/HEAD/5FlexibleFunctions.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/editorscut/ec007-swift-kickstart/HEAD/3VariablesAndConstants.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /1BeforeWeBegin.playground/Pages/Second Page.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Second Page 2 | //: [Table of Contents](TOC) | [Previous](@previous) | Next 3 | import Foundation 4 | 5 | var str = "Hello, playground" 6 | 7 | print(str) 8 | //: [Table of Contents](TOC) | [Previous](@previous) | Next 9 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/No Parameter, No Return.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### No Parameter, No Return 2 | //: [TOC](TOC) | Previous | [Next](@next) 3 | 4 | 5 | func hello() { 6 | print("Hello, World!") 7 | } 8 | 9 | hello() 10 | 11 | //: [TOC](TOC) | Previous | [Next](@next) 12 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Access Levels.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Access Levels 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | let pointThreeFour = Vertex(x:3.0, y: 4.0) 4 | let movedPoint = pointThreeFour.moveByX(8.0) 5 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # CocoaPods 2 | # 3 | # We recommend against adding the Pods directory to your .gitignore. However 4 | # you should judge for yourself, the pros and cons are mentioned at: 5 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control? 6 | # 7 | # Pods/ 8 | 9 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Return Values.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Return Values 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | func hello(name: String) -> String { 6 | return "Hello, \(name)!" 7 | } 8 | 9 | hello("my friend") 10 | 11 | 12 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 13 | -------------------------------------------------------------------------------- /1BeforeWeBegin.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Var.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Var 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let person = "Swift Programmer" 5 | var greeting = "Hello, " 6 | greeting = "Hi there, " 7 | greeting + person 8 | 9 | 10 | 11 | 12 | 13 | 14 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 15 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Function Parameters.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Function Parameters 2 | //: [TOC](TOC) | Previous | [Next](@next) 3 | 4 | 5 | var two = 2 6 | 7 | func double(var input: Int) { 8 | input 9 | input = input * 2 10 | } 11 | 12 | double(two) 13 | two 14 | 15 | 16 | //: [TOC](TOC) | Previous | [Next](@next) 17 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/TOC.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | /*: 2 | ### Chapter 3: Variables and Constants 3 | * [Inferring Type](Inferring%20Type) 4 | * [Let](Let) 5 | * [Var](Var) 6 | * [Types](Types) 7 | * [Numeric Types](Numeric%20Types) 8 | * [Nil](Nil) 9 | * [Higher Order Functions](Higher%20Order%20Functions) 10 | * [Type Alias](Type%20Alias) 11 | */ 12 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Composition.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Composition 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let point = Vertex(x: 3.0, y: 4.0) 5 | 6 | let rectangle = Rectangle(topLeftCorner: point, width: 200.0, height: 100.0) 7 | 8 | let movedRectangle = rectangle.moveByX(7.0) 9 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 10 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Overloading.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Overloading 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | func hello() { 6 | hello("World") 7 | } 8 | 9 | func hello(name: String) { 10 | print("Hello, \(name)!") 11 | } 12 | 13 | hello() 14 | hello("Swift Programmer") 15 | 16 | 17 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 18 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Inout Parameters.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Inout Parameters 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | var two = 2 6 | 7 | func double(inout input: Int) { 8 | input = input * 2 9 | } 10 | 11 | two 12 | double(&two) 13 | two 14 | double(&two) 15 | two 16 | 17 | 18 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 19 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Return Values.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Return Values 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | var two = 2 6 | 7 | func double(input: Int) -> Int { 8 | return input * 2 9 | } 10 | 11 | two 12 | double(two) 13 | two 14 | two = double(two) 15 | two 16 | 17 | 18 | 19 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 20 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Self.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Self 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let point = Vertex(x: 3.0, y: 4.0) 5 | let movedPoint = point.moveByX(7.0) 6 | let rectangle = Rectangle(topLeftCorner: point, width: 200.0, height: 100.0) 7 | let movedRectangle = rectangle.moveByX(12.0) 8 | 9 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 10 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Single Parameter.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Single Parameter 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | func hello() { 6 | print("Hello, World!") 7 | } 8 | func hello(name: String) { 9 | print("Hello, \(name)!") 10 | } 11 | 12 | hello() 13 | hello("Swift Programmer") 14 | 15 | 16 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 17 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Self.xcplaygroundpage/Sources/Size.swift: -------------------------------------------------------------------------------- 1 | struct Size { 2 | let width,height: Double 3 | } 4 | 5 | extension Size: CustomStringConvertible { 6 | var description: String { 7 | return "\(width) by \(height)" 8 | } 9 | } 10 | 11 | extension Size: Equatable{} 12 | func ==(lhs: Size, rhs: Size) -> Bool { 13 | return lhs.width == rhs.width && lhs.height == rhs.height 14 | } -------------------------------------------------------------------------------- /7Structs.playground/Pages/Composition.xcplaygroundpage/Sources/Size.swift: -------------------------------------------------------------------------------- 1 | struct Size { 2 | let width,height: Double 3 | } 4 | 5 | extension Size: CustomStringConvertible { 6 | var description: String { 7 | return "\(width) by \(height)" 8 | } 9 | } 10 | 11 | extension Size: Equatable{} 12 | func ==(lhs: Size, rhs: Size) -> Bool { 13 | return lhs.width == rhs.width && lhs.height == rhs.height 14 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Casting.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Casting 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let point = Vertex(x: 3.0, y: 4.0) 5 | let movedPoint = point.moveByX(7.0) 6 | let rectangle = Rectangle(topLeftCorner: point, width: 200.0, height: 100.0) 7 | let movedRectangle = rectangle.moveByX(12.0) 8 | 9 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 10 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Casting.xcplaygroundpage/Sources/Size.swift: -------------------------------------------------------------------------------- 1 | struct Size { 2 | let width,height: Double 3 | } 4 | 5 | extension Size: CustomStringConvertible { 6 | var description: String { 7 | return "\(width) by \(height)" 8 | } 9 | } 10 | 11 | extension Size: Equatable{} 12 | func ==(lhs: Size, rhs: Size) -> Bool { 13 | return lhs.width == rhs.width && lhs.height == rhs.height 14 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Generics.xcplaygroundpage/Sources/Size.swift: -------------------------------------------------------------------------------- 1 | struct Size { 2 | let width,height: Double 3 | } 4 | 5 | extension Size: CustomStringConvertible { 6 | var description: String { 7 | return "\(width) by \(height)" 8 | } 9 | } 10 | 11 | extension Size: Equatable{} 12 | func ==(lhs: Size, rhs: Size) -> Bool { 13 | return lhs.width == rhs.width && lhs.height == rhs.height 14 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Properties.xcplaygroundpage/Sources/Size.swift: -------------------------------------------------------------------------------- 1 | struct Size { 2 | let width,height: Double 3 | } 4 | 5 | extension Size: CustomStringConvertible { 6 | var description: String { 7 | return "\(width) by \(height)" 8 | } 9 | } 10 | 11 | extension Size: Equatable{} 12 | func ==(lhs: Size, rhs: Size) -> Bool { 13 | return lhs.width == rhs.width && lhs.height == rhs.height 14 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Custom Operators.xcplaygroundpage/Sources/Size.swift: -------------------------------------------------------------------------------- 1 | struct Size { 2 | let width,height: Double 3 | } 4 | 5 | extension Size: CustomStringConvertible { 6 | var description: String { 7 | return "\(width) by \(height)" 8 | } 9 | } 10 | 11 | extension Size: Equatable{} 12 | func ==(lhs: Size, rhs: Size) -> Bool { 13 | return lhs.width == rhs.width && lhs.height == rhs.height 14 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Polymorphism.xcplaygroundpage/Sources/Size.swift: -------------------------------------------------------------------------------- 1 | struct Size { 2 | let width,height: Double 3 | } 4 | 5 | extension Size: CustomStringConvertible { 6 | var description: String { 7 | return "\(width) by \(height)" 8 | } 9 | } 10 | 11 | extension Size: Equatable{} 12 | func ==(lhs: Size, rhs: Size) -> Bool { 13 | return lhs.width == rhs.width && lhs.height == rhs.height 14 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Creating a Protocol.xcplaygroundpage/Sources/Size.swift: -------------------------------------------------------------------------------- 1 | struct Size { 2 | let width,height: Double 3 | } 4 | 5 | extension Size: CustomStringConvertible { 6 | var description: String { 7 | return "\(width) by \(height)" 8 | } 9 | } 10 | 11 | extension Size: Equatable{} 12 | func ==(lhs: Size, rhs: Size) -> Bool { 13 | return lhs.width == rhs.width && lhs.height == rhs.height 14 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Protocol Extensions.xcplaygroundpage/Sources/Size.swift: -------------------------------------------------------------------------------- 1 | struct Size { 2 | let width,height: Double 3 | } 4 | 5 | extension Size: CustomStringConvertible { 6 | var description: String { 7 | return "\(width) by \(height)" 8 | } 9 | } 10 | 11 | extension Size: Equatable{} 12 | func ==(lhs: Size, rhs: Size) -> Bool { 13 | return lhs.width == rhs.width && lhs.height == rhs.height 14 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Conforming to a Protocol.xcplaygroundpage/Sources/Size.swift: -------------------------------------------------------------------------------- 1 | struct Size { 2 | let width,height: Double 3 | } 4 | 5 | extension Size: CustomStringConvertible { 6 | var description: String { 7 | return "\(width) by \(height)" 8 | } 9 | } 10 | 11 | extension Size: Equatable{} 12 | func ==(lhs: Size, rhs: Size) -> Bool { 13 | return lhs.width == rhs.width && lhs.height == rhs.height 14 | } -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Creating an Enumeration.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Creating an Enumeration 2 | //: [TOC](TOC) | Previous | [Next](@next) 3 | 4 | 5 | enum Color { 6 | case Red 7 | case Green 8 | case Blue 9 | } 10 | 11 | var crayon = Color.Red 12 | crayon = .Green 13 | 14 | let paintBrush: Color 15 | 16 | // much later 17 | 18 | paintBrush = .Blue 19 | 20 | //: [TOC](TOC) | Previous | [Next](@next) 21 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Conforming to a Protocol.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Conforming to a Protocol 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | //: This section has an error in Rectangle.swift that has been commented out. The error is fixed in the next section. 5 | 6 | let point = Vertex(x: 3.0, y: 4.0) 7 | let movedPoint = point.moveByX(7.0) 8 | 9 | 10 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 11 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/TOC.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | /*: 2 | ### Chapter 8: Protocols 3 | * [Creating a Protocol](Creating%20a%20Protocol) 4 | * [Conforming to a Protocol](Conforming%20to%20a%20Protocol) 5 | * [Casting](Casting) 6 | * [Self](Self) 7 | * [Properties](Properties) 8 | * [Polymorphism](Polymorphism) 9 | * [Generics](Generics) 10 | * [Protocol Extensions](Protocol%20Extensions) 11 | * [Custom Operators](Custom%20Operators) 12 | */ 13 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Numeric Types.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Numeric Types 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let simpleProduct = 5 * 3.4 5 | 6 | let five = 5 7 | let threePointFour = 3.4 8 | //let product = five * threePointFour 9 | 10 | let fiveAsDouble = Double(5) 11 | 12 | let product = fiveAsDouble * threePointFour 13 | 14 | 15 | 16 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 17 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/String Raw Values.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### String Raw Values 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | enum Color: String { 5 | case Red = "Maraschino" 6 | case Green 7 | case Blue = "Blueberry" 8 | } 9 | 10 | 11 | let crayon = Color.Green 12 | crayon.rawValue 13 | 14 | let paintBrush = Color(rawValue: "Blueberry") 15 | 16 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 17 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Modify Dictionaries.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Modify Dictionaries 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | var numbers = [String:Int]() 5 | numbers["one"] = 3 6 | numbers["too"] = 4 7 | numbers["one"] = 1 8 | numbers["two"] = 2 9 | numbers.removeValueForKey("too") 10 | numbers.removeValueForKey("four") 11 | numbers.count 12 | numbers 13 | 14 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 15 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Properties.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Properties 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let point = Vertex(x: 3.0, y: 4.0) 5 | let movedPoint = point.moveByX(7.0) 6 | point.location 7 | let rectangle = Rectangle(topLeftCorner: point, width: 200.0, height: 100.0) 8 | rectangle.location 9 | let movedRectangle = rectangle.moveByX(12.0) 10 | 11 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 12 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/The print() Function.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### The print() Function 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | let name = "Swift Programmer" 4 | 5 | print("Hello, World!") 6 | print("Hello, \(name)!") 7 | print("Hello,", name, "!") 8 | 9 | print("Hello,", name, "!", separator:"^-^") 10 | 11 | print("Hello, ", name, "!", separator: "", terminator: "") 12 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 13 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Generics.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Generics 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | func modify(input: T, byApplying f: (T) -> T) -> T { 6 | return f(input) 7 | } 8 | 9 | let two = modify(1){input in input * 2} 10 | 11 | let three = modify(1.5){input in input * 2.0} 12 | 13 | let greeting = modify("Hello"){input in input + ", World!"} 14 | 15 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 16 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Custom String Convertible.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Custom String Convertible 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | struct Vertex { 5 | let x, y: Double 6 | } 7 | 8 | extension Vertex: CustomStringConvertible { 9 | var description: String { 10 | return "(\(x), \(y))" 11 | } 12 | } 13 | 14 | let pointThreeFour = Vertex(x: 3.0, y: 4.0) 15 | 16 | 17 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 18 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/By Value.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### By Value 2 | //: [TOC](TOC) | [Previous](@previous) | Next 3 | 4 | struct Vertex { 5 | var x, y: Double 6 | } 7 | 8 | extension Vertex: CustomStringConvertible { 9 | var description: String { 10 | return "(\(x), \(y))" 11 | } 12 | } 13 | var pointOne = Vertex(x: 3.0, y: 4.0) 14 | var pointTwo = pointOne 15 | pointOne.x = 6.0 16 | pointTwo.y = 8.0 17 | pointOne 18 | //: [TOC](TOC) | [Previous](@previous) | Next 19 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Custom Operators.xcplaygroundpage/Sources/Movable.swift: -------------------------------------------------------------------------------- 1 | public protocol Movable { 2 | var location: Vertex {get} 3 | func moveByX(deltaX: Double) -> Self 4 | func shiftRight() -> Self 5 | func whereAmI() -> Vertex 6 | } 7 | 8 | extension Movable { 9 | public func shiftRight() -> Self { 10 | return moveByX(1) 11 | } 12 | } 13 | 14 | 15 | extension Movable { 16 | public func whereAmI() -> Vertex { 17 | return self.location 18 | } 19 | } -------------------------------------------------------------------------------- /7Structs.playground/Pages/TOC.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | /*: 2 | ### Chapter 7: Structs 3 | * [Creating a Struct](Creating%20a%20Struct) 4 | * [Stored Properties](Stored%20Properties) 5 | * [Computed Properties](Computed%20Properties) 6 | * [Will Set Did Set](Will%20Set%20Did%20Set) 7 | * [Custom String Convertible](Custom%20String%20Convertible) 8 | * [Equatable](Equatable) 9 | * [Methods](Methods) 10 | * [Access Levels](Access%20Levels) 11 | * [Composition](Composition) 12 | * [By Value](By%20Value) 13 | */ 14 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Default Values.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Default Values 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | func hello(name: String = "World", numberOfTimes: Int = 1) { 6 | for _ in 1 ... numberOfTimes { 7 | print("Hello, \(name)!") 8 | } 9 | } 10 | 11 | hello() 12 | hello("Swift Programmer") 13 | hello("Swift 2 Adopter", numberOfTimes: 3) 14 | hello(numberOfTimes:2) 15 | 16 | 17 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 18 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Reference Types.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Reference Types 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | import UIKit 6 | 7 | var textField = UITextField() 8 | textField.text = "Hello" 9 | textField.text 10 | 11 | func modifyField(field: UITextField) { 12 | field.text 13 | field.text = "Goodbye" 14 | field.text 15 | } 16 | modifyField(textField) 17 | textField.text 18 | 19 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 20 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Generics.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Generics 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | func shiftLeft(movable: T) -> T { 5 | return movable.moveByX(-1) 6 | } 7 | 8 | let point = Vertex(x: 3.0, y: 4.0) 9 | let shiftedLeftPoint = shiftLeft(point) 10 | let rectangle = Rectangle(topLeftCorner: point, width: 200.0, height: 100.0) 11 | let shiftedLeftRectangle = shiftLeft(rectangle) 12 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 13 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Protocol Extensions.xcplaygroundpage/Sources/Movable.swift: -------------------------------------------------------------------------------- 1 | public protocol Movable { 2 | var location: Vertex {get} 3 | func moveByX(deltaX: Double) -> Self 4 | func shiftRight() -> Self 5 | func whereAmI() -> Vertex 6 | } 7 | 8 | extension Movable { 9 | public func shiftRight() -> Self { 10 | return moveByX(1) 11 | } 12 | } 13 | 14 | 15 | 16 | extension Movable { 17 | public func whereAmI() -> Vertex { 18 | return self.location 19 | } 20 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Polymorphism.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Polymorphism 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | func shiftLeft(movable: Movable) -> Movable { 5 | return movable.moveByX(-1) 6 | } 7 | 8 | let point = Vertex(x: 3.0, y: 4.0) 9 | let shiftedLeftPoint = shiftLeft(point) 10 | let rectangle = Rectangle(topLeftCorner: point, width: 200.0, height: 100.0) 11 | let shiftedLeftRectangle = shiftLeft(rectangle) 12 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 13 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Variadic Parameters.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Variadic Parameters 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | func hello(peopleNamed people: String...) { 6 | if people.isEmpty { 7 | hello(peopleNamed: "World") 8 | } 9 | for person in people { 10 | print("Hello, \(person)!") 11 | } 12 | } 13 | 14 | hello(peopleNamed: "Swift Programmer", "Swift 2 Developer") 15 | hello() 16 | 17 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 18 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Returning Tuples.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Returning Tuples 2 | //: [TOC](TOC) | [Previous](@previous) | Next 3 | 4 | 5 | func hello(names: String...) -> (count: Int, peopleList: String) { 6 | return (names.count, names.reduce("Hello,"){$0 + "\n " + $1}) 7 | } 8 | 9 | 10 | hello("Swift Programmer", "Swift 2 Developer") 11 | 12 | hello("Swift Programmer", "Swift 2 Developer").count 13 | hello("Swift Programmer", "Swift 2 Developer").peopleList 14 | 15 | 16 | //: [TOC](TOC) | [Previous](@previous) | Next 17 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/By Value.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### By Value 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | enum PrimaryColor: String { 5 | case Red 6 | case Yellow 7 | case Blue 8 | } 9 | 10 | var color1 = PrimaryColor.Red 11 | var color2 = color1 12 | 13 | color1 = .Yellow 14 | color1 15 | color2 16 | 17 | color2 = .Blue 18 | color1 19 | color2 20 | 21 | color1 == color2 22 | 23 | color1 = .Blue 24 | 25 | color1 == color2 26 | 27 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 28 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/TOC.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | /*: 2 | ### Chapter 5: Flexible Functions 3 | * [Function Parameters](Function%20Parameters) 4 | * [Reference Types](Reference%20Types) 5 | * [Inout Parameters](Inout%20Parameters) 6 | * [Return Values](Return%20Values) 7 | * [Extensions](Extensions) 8 | * [Higher Order Functions](Higher%20Order%20Functions) 9 | * [Closure Expressions](Closure%20Expressions) 10 | * [Generics](Generics) 11 | * [Transforming Arrays](Transforming%20Arrays) 12 | * [Understanding Map](Understanding%20Map) 13 | */ 14 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/TOC.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | /*: 2 | ### Chapter 2: Functions 3 | * [No Parameter, No Return](No%20Parameter,%20No%20Return) 4 | * [Single Parameter](Single%20Parameter) 5 | * [Overloading](Overloading) 6 | * [Multiple Parameters](Multiple%20Parameters) 7 | * [Default Values](Default%20Values) 8 | * [External Names](External%20Names) 9 | * [The print() Function](The%20print()%20Function) 10 | * [Variadic Parameters](Variadic%20Parameters) 11 | * [Return Values](Return%20Values) 12 | * [Returning Tuples](Returning%20Tuples) 13 | */ 14 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Extensions.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Extensions 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | extension Int { 6 | mutating func doubleInPlace() { 7 | self = self * 2 8 | } 9 | } 10 | var two = 2 11 | two.doubleInPlace() 12 | two 13 | 14 | extension Int { 15 | func double() -> Int { 16 | return self * 2 17 | } 18 | } 19 | 20 | two 21 | two.double() 22 | two 23 | two = two.double() 24 | two 25 | 26 | 27 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 28 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Let.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Let 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let person = "Swift Programmer" 5 | 6 | // can't do this person = "some new value" 7 | 8 | let shouldBeRed: Bool 9 | 10 | shouldBeRed = true 11 | 12 | let stringWithoutValue: String 13 | 14 | if shouldBeRed { 15 | stringWithoutValue = "red" 16 | } else { 17 | stringWithoutValue = "blue" 18 | } 19 | 20 | stringWithoutValue 21 | 22 | 23 | 24 | 25 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 26 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/TOC.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | /*: 2 | ### Chapter 4: Collections 3 | * [One Type](One%20Type) 4 | * [Create Arrays](Create%20Arrays) 5 | * [Modify Arrays](Modify%20Arrays) 6 | * [Enumerate Arrays](Enumerate%20Arrays) 7 | * [Value and Reference Types](Value%20and%20Reference%20Types) 8 | * [Create Dictionaries](Create%20Dictionaries) 9 | * [Modify Dictionaries](Modify%20Dictionaries) 10 | * [Optionals and Dictionaries](Optionals%20and%20Dictionaries) 11 | * [Enumerate Dictionaries](Enumerate%20Dictionaries) 12 | * [Sets](Sets) 13 | */ 14 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/One Type.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### One Type 2 | //: [TOC](TOC) | Previous | [Next](@next) 3 | 4 | let numbers = [0, 1, 2] 5 | 6 | let three = numbers[1] + numbers[2] 7 | 8 | //: The following is meant to illustrate why we don't create non-homogeneous arrays. Please, do not write code like this. 9 | 10 | import Foundation 11 | let badNumbers = [0, 1, 2, "Hello"] 12 | 13 | let badThree = (badNumbers[0] as! NSNumber).integerValue + (badNumbers[1] as! NSNumber).integerValue 14 | 15 | 16 | //: [TOC](TOC) | Previous | [Next](@next) 17 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Enumerate Arrays.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Enumerate Arrays 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let coffeeDrinks = ["Drip", "Espresso", "Americano", "Cappuccino"] 5 | 6 | for index in 0 ..< coffeeDrinks.count { 7 | print(coffeeDrinks[index]) 8 | } 9 | 10 | for kindOfDrink in coffeeDrinks { 11 | print(kindOfDrink) 12 | } 13 | 14 | for (index, kindOfDrink) in coffeeDrinks.enumerate() { 15 | print("(\(index + 1)) " + kindOfDrink) 16 | } 17 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 18 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Initializers.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Initializers 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | class ConferenceAttendee { 5 | let name, hometown: String 6 | init(name: String, hometown: String = "Cupertino") { 7 | self.name = name 8 | self.hometown = hometown 9 | } 10 | } 11 | 12 | let daniel = ConferenceAttendee(name: "Daniel", hometown: "Shaker Heights") 13 | let kimberli = ConferenceAttendee(name: "Kimberli") 14 | kimberli.hometown 15 | 16 | 17 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 18 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Creating a Class.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Creating a Class 2 | //: [TOC](TOC) | Previous | [Next](@next) 3 | 4 | class ConferenceAttendee { 5 | var name = "Daniel" 6 | var hometown = "Shaker Heights" 7 | } 8 | 9 | let attendee = ConferenceAttendee() 10 | let otherAttendee = attendee 11 | 12 | attendee.name 13 | attendee.hometown 14 | attendee.name = "Danny" 15 | otherAttendee.hometown = "Cleveland" 16 | attendee.name 17 | attendee.hometown 18 | otherAttendee.name 19 | otherAttendee.hometown 20 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 21 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Methods.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Stored Properties.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Stored Properties 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | struct Vertex { 5 | let x, y: Double 6 | 7 | init(x: Double = 3.0, y: Double = 4.0) { 8 | self.x = x 9 | self.y = y 10 | } 11 | init(bothXandY t: Double) { 12 | self.init(x:t, y: t) 13 | } 14 | } 15 | 16 | let pointThreeFour = Vertex() 17 | let pointSevenEight = Vertex(x: 7.0, y: 8.0) 18 | let pointFiveFive = Vertex(bothXandY: 5.0) 19 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 20 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/TOC.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | /*: 2 | ### Chapter 9: Classes 3 | * [Creating a Class](Creating%20a%20Class) 4 | * [Initializers](Initializers) 5 | * [Subclasses](Subclasses) 6 | * [Methods](Methods) 7 | * [Optional Property Values](Optional%20Property%20Values) 8 | * [Failable Initializers](Failable%20Initializers) 9 | * [Required Initializers](Required%20Initializers) 10 | * [Optional Chaining](Optional%20Chaining) 11 | * [Loading and Unloading](Loading%20and%20Unloading) 12 | * [Composition Preparation](Composition%20Preparation) 13 | * [Composition](Composition) 14 | */ 15 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Var.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Create Arrays.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Generics.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Option Sets.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Option Sets 2 | //: [TOC](TOC) | [Previous](@previous) | Next 3 | struct ColorsWeLove: OptionSetType { 4 | let rawValue: Int 5 | static let Red = ColorsWeLove(rawValue: 1) 6 | static let Green = ColorsWeLove(rawValue: 2) 7 | static let Blue = ColorsWeLove(rawValue: 4) 8 | } 9 | 10 | var noColors: ColorsWeLove = [] 11 | noColors.contains(.Red) 12 | 13 | var twoColors: ColorsWeLove = [.Red, .Blue] 14 | twoColors.contains(.Red) 15 | twoColors.contains(.Green) 16 | 17 | //: [TOC](TOC) | [Previous](@previous) | Next 18 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Associated Values.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Associated Values 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | enum PrimaryColor { 5 | case Red 6 | case Yellow 7 | case Blue 8 | } 9 | 10 | enum Desktop { 11 | case Black 12 | case White 13 | case Color(PrimaryColor) 14 | } 15 | 16 | let blackBackground = Desktop.Black 17 | let whiteBackground = Desktop.White 18 | 19 | let redBackground = Desktop.Color(PrimaryColor.Red) 20 | let yellowBackground = Desktop.Color(.Yellow) 21 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 22 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Enumerate Dictionaries.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Enumerate Dictionaries 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let numbers = ["one":1, "two":2] 5 | 6 | for numberKey in numbers.keys { 7 | print("The value of \(numberKey) is \(numbers[numberKey]!).") 8 | } 9 | 10 | for number in numbers { 11 | print("The value of \(number.0) is \(number.1).") 12 | } 13 | 14 | for (spelledOutNumber, numeral) in numbers { 15 | print("The value of \(spelledOutNumber) is \(numeral).") 16 | } 17 | 18 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 19 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/TOC.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | /*: 2 | ### Chapter 6: Enumerations 3 | * [Creating an Enumeration](Creating%20an%20Enumeration) 4 | * [The Switch Statement](The%20Switch%20Statement) 5 | * [Methods](Methods) 6 | * [Computed Properties](Computed%20Properties) 7 | * [String Raw Values](String%20Raw%20Values) 8 | * [Numerical Raw Values](Numerical%20Raw%20Values) 9 | * [Associated Values](Associated%20Values) 10 | * [Binding to Associated Values](Binding%20to%20Associated%20Values) 11 | * [By Value](By%20Value) 12 | * [Error Type](Error%20Type) 13 | * [Option Sets](Option%20Sets) 14 | */ 15 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Composition.xcplaygroundpage/Sources/Vertex.swift: -------------------------------------------------------------------------------- 1 | public struct Vertex { 2 | let x,y: Double 3 | 4 | public init(x: Double, y: Double) { 5 | self.x = x 6 | self.y = y 7 | } 8 | 9 | public func moveByX(deltaX: Double) -> Vertex { 10 | return Vertex(x: x + deltaX, y: y) 11 | } 12 | } 13 | 14 | extension Vertex: CustomStringConvertible { 15 | public var description: String { 16 | return "(\(x), \(y))" 17 | } 18 | } 19 | 20 | extension Vertex: Equatable{} 21 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 22 | return lhs.x == rhs.x && lhs.y == rhs.y 23 | } -------------------------------------------------------------------------------- /4Collections.playground/Pages/Create Dictionaries.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Create Dictionaries 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let numbers = ["one":1, "two":2, "three":3] 5 | var moreNumbers: [String: Int] 6 | 7 | let numbersKeys = numbers.keys 8 | let numbersValues = numbers.values 9 | 10 | for number in numbers.keys { 11 | print("\(number) : \(numbers[number])") 12 | } 13 | 14 | print("\n") // just creating some space in the output 15 | 16 | for number in numbers.keys { 17 | print("\(number) : \(numbers[number]!)") 18 | } 19 | 20 | 21 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 22 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Access Levels.xcplaygroundpage/Sources/Vertex.swift: -------------------------------------------------------------------------------- 1 | public struct Vertex { 2 | let x,y: Double 3 | 4 | public init(x: Double, y: Double) { 5 | self.x = x 6 | self.y = y 7 | } 8 | 9 | public func moveByX(deltaX: Double) -> Vertex { 10 | return Vertex(x: x + deltaX, y: y) 11 | } 12 | } 13 | 14 | extension Vertex: CustomStringConvertible { 15 | public var description: String { 16 | return "(\(x), \(y))" 17 | } 18 | } 19 | 20 | extension Vertex: Equatable{} 21 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 22 | return lhs.x == rhs.x && lhs.y == rhs.y 23 | } -------------------------------------------------------------------------------- /8Protocols.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Creating a Protocol.xcplaygroundpage/Sources/Vertex.swift: -------------------------------------------------------------------------------- 1 | public struct Vertex { 2 | let x,y: Double 3 | 4 | public init(x: Double, y: Double) { 5 | self.x = x 6 | self.y = y 7 | } 8 | 9 | public func moveByX(deltaX: Double) -> Vertex { 10 | return Vertex(x: x + deltaX, y: y) 11 | } 12 | } 13 | 14 | extension Vertex: CustomStringConvertible { 15 | public var description: String { 16 | return "(\(x), \(y))" 17 | } 18 | } 19 | 20 | extension Vertex: Equatable{} 21 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 22 | return lhs.x == rhs.x && lhs.y == rhs.y 23 | } -------------------------------------------------------------------------------- /7Structs.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /1BeforeWeBegin.playground/playground.xcworkspace/xcuserdata/dimsum.xcuserdatad/WorkspaceSettings.xcsettings: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | BuildLocationStyle 6 | UseAppPreferences 7 | CustomBuildLocationType 8 | RelativeToDerivedData 9 | DerivedDataLocationStyle 10 | Default 11 | IssueFilterStyle 12 | ShowActiveSchemeOnly 13 | LiveSourceIssuesEnabled 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Higher Order Functions.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Higher Order Functions 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | let two = 2 6 | 7 | func double(input: Int) -> Int { 8 | return input * 2 9 | } 10 | 11 | let four = double(two) 12 | 13 | func modify(input: Int, byApplying f: (Int) -> Int) -> Int { 14 | return f(input) 15 | } 16 | 17 | let eight = modify(four, byApplying: double) 18 | 19 | func addSeven(input: Int) -> Int { 20 | return input + 7 21 | } 22 | 23 | let fifteen = modify(eight, byApplying: addSeven) 24 | 25 | 26 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 27 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Casting.xcplaygroundpage/Sources/Vertex.swift: -------------------------------------------------------------------------------- 1 | public struct Vertex { 2 | let x,y: Double 3 | 4 | public init(x: Double, y: Double) { 5 | self.x = x 6 | self.y = y 7 | } 8 | } 9 | 10 | extension Vertex: Movable { 11 | public func moveByX(deltaX: Double) -> Movable { 12 | return Vertex(x: x + deltaX, y: y) 13 | } 14 | } 15 | 16 | extension Vertex: CustomStringConvertible { 17 | public var description: String { 18 | return "(\(x), \(y))" 19 | } 20 | } 21 | 22 | extension Vertex: Equatable{} 23 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 24 | return lhs.x == rhs.x && lhs.y == rhs.y 25 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Self.xcplaygroundpage/Sources/Vertex.swift: -------------------------------------------------------------------------------- 1 | public struct Vertex { 2 | let x,y: Double 3 | 4 | public init(x: Double, y: Double) { 5 | self.x = x 6 | self.y = y 7 | } 8 | } 9 | 10 | extension Vertex: Movable { 11 | public func moveByX(deltaX: Double) -> Vertex { 12 | return Vertex(x: x + deltaX, y: y) 13 | } 14 | } 15 | 16 | extension Vertex: CustomStringConvertible { 17 | public var description: String { 18 | return "(\(x), \(y))" 19 | } 20 | } 21 | 22 | extension Vertex: Equatable{} 23 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 24 | return lhs.x == rhs.x && lhs.y == rhs.y 25 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Protocol Extensions.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Protocol Extensions 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | func shiftLeft(movable: T) -> T { 5 | return movable.moveByX(-1) 6 | } 7 | 8 | let point = Vertex(x: 3.0, y: 4.0) 9 | let shiftedLeftPoint = shiftLeft(point) 10 | let shiftedRightPoint = point.shiftRight() 11 | let rectangle = Rectangle(topLeftCorner: point, width: 200.0, height: 100.0) 12 | let shiftedLeftRectangle = shiftLeft(rectangle) 13 | let shiftedRightRectangle = rectangle.shiftRight() 14 | point.whereAmI() 15 | rectangle.whereAmI() 16 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 17 | -------------------------------------------------------------------------------- /2Functions.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /4Collections.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/External Names.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### External Names 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | func hello(name: String, affiliation: String) { 5 | print("Hello \(name) from \(affiliation).") 6 | } 7 | 8 | hello("Swift Programmer", affiliation: "Dim Sum Thinking") 9 | 10 | func hello(name name: String = "World", times numberOfTimes: Int = 1) { 11 | for _ in 1 ... numberOfTimes { 12 | print("Hello, \(name)!") 13 | } 14 | } 15 | 16 | hello() 17 | hello(name: "Swift Programmer") 18 | hello(name: "Swift 2 Adopter", times: 3) 19 | hello(times:2) 20 | 21 | 22 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 23 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Conforming to a Protocol.xcplaygroundpage/Sources/Vertex.swift: -------------------------------------------------------------------------------- 1 | public struct Vertex { 2 | let x,y: Double 3 | 4 | public init(x: Double, y: Double) { 5 | self.x = x 6 | self.y = y 7 | } 8 | } 9 | 10 | extension Vertex: Movable { 11 | public func moveByX(deltaX: Double) -> Movable { 12 | return Vertex(x: x + deltaX, y: y) 13 | } 14 | } 15 | 16 | extension Vertex: CustomStringConvertible { 17 | public var description: String { 18 | return "(\(x), \(y))" 19 | } 20 | } 21 | 22 | extension Vertex: Equatable{} 23 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 24 | return lhs.x == rhs.x && lhs.y == rhs.y 25 | } -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Transforming Arrays.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Transforming Arrays 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | func transform(input: [T], byApplying f: (T) -> U) -> [U] { 6 | var transformedArray = [U]() 7 | for element in input { 8 | transformedArray.append(f(element)) 9 | } 10 | return transformedArray 11 | } 12 | 13 | let numberOfCopies = [9, 12, 7, 15, 20] 14 | 15 | let isBiggerThanTen = transform(numberOfCopies){$0 > 10} 16 | 17 | isBiggerThanTen 18 | 19 | let revenue = transform(numberOfCopies){Double($0) * 0.99 * 0.70} 20 | 21 | revenue 22 | 23 | 24 | 25 | 26 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 27 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/The Switch Statement.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### The Switch Statement 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | import UIKit 5 | 6 | enum Color { 7 | case Red 8 | case Green 9 | case Blue 10 | } 11 | 12 | func actualColor(color: Color) -> UIColor { 13 | switch color { 14 | case .Red: 15 | return UIColor.redColor() 16 | case .Green: 17 | return UIColor.greenColor() 18 | case .Blue: 19 | return UIColor.blueColor() 20 | } 21 | } 22 | 23 | let crayon = Color.Green 24 | actualColor(crayon) 25 | 26 | let paintBrush = Color.Blue 27 | actualColor(paintBrush) 28 | 29 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 30 | -------------------------------------------------------------------------------- /6Enumerations.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Generics.xcplaygroundpage/Sources/Vertex.swift: -------------------------------------------------------------------------------- 1 | public struct Vertex { 2 | let x,y: Double 3 | 4 | public init(x: Double, y: Double) { 5 | self.x = x 6 | self.y = y 7 | } 8 | } 9 | 10 | extension Vertex: Movable { 11 | public var location: Vertex { 12 | return self 13 | } 14 | public func moveByX(deltaX: Double) -> Vertex { 15 | return Vertex(x: x + deltaX, y: y) 16 | } 17 | } 18 | 19 | extension Vertex: CustomStringConvertible { 20 | public var description: String { 21 | return "(\(x), \(y))" 22 | } 23 | } 24 | 25 | extension Vertex: Equatable{} 26 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 27 | return lhs.x == rhs.x && lhs.y == rhs.y 28 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Polymorphism.xcplaygroundpage/Sources/Vertex.swift: -------------------------------------------------------------------------------- 1 | public struct Vertex { 2 | let x,y: Double 3 | 4 | public init(x: Double, y: Double) { 5 | self.x = x 6 | self.y = y 7 | } 8 | } 9 | 10 | extension Vertex: Movable { 11 | public var location: Vertex { 12 | return self 13 | } 14 | public func moveByX(deltaX: Double) -> Vertex { 15 | return Vertex(x: x + deltaX, y: y) 16 | } 17 | } 18 | 19 | extension Vertex: CustomStringConvertible { 20 | public var description: String { 21 | return "(\(x), \(y))" 22 | } 23 | } 24 | 25 | extension Vertex: Equatable{} 26 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 27 | return lhs.x == rhs.x && lhs.y == rhs.y 28 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Properties.xcplaygroundpage/Sources/Vertex.swift: -------------------------------------------------------------------------------- 1 | public struct Vertex { 2 | let x,y: Double 3 | 4 | public init(x: Double, y: Double) { 5 | self.x = x 6 | self.y = y 7 | } 8 | } 9 | 10 | extension Vertex: Movable { 11 | public var location: Vertex { 12 | return self 13 | } 14 | public func moveByX(deltaX: Double) -> Vertex { 15 | return Vertex(x: x + deltaX, y: y) 16 | } 17 | } 18 | 19 | extension Vertex: CustomStringConvertible { 20 | public var description: String { 21 | return "(\(x), \(y))" 22 | } 23 | } 24 | 25 | extension Vertex: Equatable{} 26 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 27 | return lhs.x == rhs.x && lhs.y == rhs.y 28 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Protocol Extensions.xcplaygroundpage/Sources/Vertex.swift: -------------------------------------------------------------------------------- 1 | public struct Vertex { 2 | let x,y: Double 3 | 4 | public init(x: Double, y: Double) { 5 | self.x = x 6 | self.y = y 7 | } 8 | } 9 | 10 | extension Vertex: Movable { 11 | public var location: Vertex { 12 | return self 13 | } 14 | public func moveByX(deltaX: Double) -> Vertex { 15 | return Vertex(x: x + deltaX, y: y) 16 | } 17 | } 18 | 19 | extension Vertex: CustomStringConvertible { 20 | public var description: String { 21 | return "(\(x), \(y))" 22 | } 23 | } 24 | 25 | extension Vertex: Equatable{} 26 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 27 | return lhs.x == rhs.x && lhs.y == rhs.y 28 | } -------------------------------------------------------------------------------- /9Classes.playground/contents.xcplayground: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Closure Expressions.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Closure Expressions 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | func modify(input: Int, byApplying f: (Int) -> Int) -> Int { 6 | return f(input) 7 | } 8 | 9 | let four = modify(2, byApplying: {(input: Int) -> Int in 10 | return input * 2 11 | }) 12 | 13 | let six = modify(3){(input: Int) -> Int in 14 | return input * 2 15 | } 16 | let eight = modify(4){input in return input * 2} 17 | 18 | let ten = modify(5){input in input * 2} 19 | 20 | let twelve = modify(6){$0 * 2} 21 | 22 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 23 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Nil.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Nil 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | var name: String? 5 | name = "Daniel" 6 | print(name!) 7 | name = nil 8 | // this produces a runtime error print(name!) 9 | 10 | import UIKit 11 | 12 | let image = UIImage(named: "Kickstart.jpg") 13 | let imageView = UIImageView() 14 | 15 | if image != nil { 16 | let validImage = image! 17 | imageView.image = validImage 18 | } else { 19 | print("There is no image named Kickstart.jpg") 20 | } 21 | 22 | if let validImage = image { 23 | imageView.image = validImage 24 | } else { 25 | print("There is no image named Kickstart.jpg") 26 | } 27 | 28 | 29 | imageView.image 30 | 31 | 32 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 33 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Types.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Types 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let person = "Swift Programmer" 5 | var greeting = "Hello, " 6 | greeting = "Hi there, " 7 | // can't change the type of greeting greeting = 7 8 | greeting + person 9 | 10 | 11 | 12 | func hello(name: String, numberOfTimes: Int) -> String { 13 | var tempGreeting = "" 14 | for _ in 1 ... numberOfTimes { 15 | tempGreeting += "Hello, \(name)!\n" 16 | } 17 | return tempGreeting 18 | } 19 | 20 | 21 | greeting = hello(person, numberOfTimes: 3) 22 | 23 | hello 24 | 25 | let greetingFunction = hello 26 | 27 | greeting = greetingFunction("Surprised Reader", numberOfTimes: 4) 28 | 29 | 30 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 31 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Computed Properties.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Computed Properties 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | import Foundation 4 | 5 | struct Vertex { 6 | private(set) var x, y: Double 7 | 8 | var magnitude: Double { 9 | get { 10 | return sqrt(x * x + y * y) 11 | } 12 | set { 13 | let multiplier = newValue / magnitude 14 | x *= multiplier 15 | y *= multiplier 16 | } 17 | } 18 | 19 | init(x: Double = 3.0, y: Double = 4.0) { 20 | self.x = x 21 | self.y = y 22 | } 23 | 24 | } 25 | 26 | var pointThreeFour = Vertex() 27 | pointThreeFour.magnitude 28 | pointThreeFour.magnitude = 10.0 29 | pointThreeFour 30 | 31 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 32 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Equatable.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Equatable 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | struct Vertex { 5 | let x, y: Double 6 | } 7 | 8 | extension Vertex: CustomStringConvertible { 9 | var description: String { 10 | return "(\(x), \(y))" 11 | } 12 | } 13 | 14 | extension Vertex: Equatable{} 15 | func ==(lhs: Vertex, rhs: Vertex) -> Bool { 16 | return lhs.x == rhs.x && lhs.y == rhs.y 17 | } 18 | 19 | let pointThreeFour = Vertex(x: 3.0, y: 4.0) 20 | let pointFiveSix = Vertex(x: 5.0, y: 6.0) 21 | let anotherPointThreeFour = Vertex(x: 3.0, y: 4.0) 22 | 23 | pointThreeFour == anotherPointThreeFour 24 | pointThreeFour == pointFiveSix 25 | 26 | pointThreeFour != anotherPointThreeFour 27 | pointThreeFour != pointFiveSix 28 | 29 | 30 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 31 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Sets.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Sets 2 | //: [TOC](TOC) | [Previous](@previous) | Next 3 | 4 | var odds = Set() 5 | for i in 1.stride(through: 10, by: 2) { 6 | odds.insert(i) 7 | } 8 | odds.insert(7) 9 | //odds.remove(7) 10 | odds.contains(7) 11 | 12 | var primes: Set = [2,3,5,7] 13 | 14 | let union = primes.union(odds) 15 | let intersection = primes.intersect(odds) 16 | let difference = odds.subtract(primes) 17 | let symmetricDifference = odds.exclusiveOr(primes) 18 | 19 | odds.intersectInPlace(primes) 20 | 21 | intersection.isDisjointWith(union) 22 | intersection.isSubsetOf(union) 23 | intersection.isStrictSubsetOf(union) 24 | 25 | for prime in primes { 26 | print("\(prime)") 27 | } 28 | 29 | 30 | odds.count 31 | odds 32 | primes 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | //: [TOC](TOC) | [Previous](@previous) | Next 41 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Loading and Unloading.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Loading and Unloading 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | class ConferenceLocation { 5 | let city: String 6 | 7 | init(city: String = "San Francisco"){ 8 | self.city = city 9 | print("Creating Conference Location") 10 | } 11 | } 12 | 13 | 14 | class ConferenceAttendee { 15 | let name: String 16 | lazy var location = ConferenceLocation() 17 | 18 | init(name: String) { 19 | self.name = name 20 | print("Creating Conference Attendee") 21 | } 22 | deinit { 23 | // cleanup resources 24 | } 25 | } 26 | 27 | let daniel = ConferenceAttendee(name: "Daniel") 28 | daniel.location 29 | 30 | typealias StreetNumberAndName = String 31 | 32 | 33 | 34 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 35 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Understanding Map.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Understanding Map 2 | //: [TOC](TOC) | [Previous](@previous) | Next 3 | 4 | extension Array { 5 | func transform(f:(Element) -> U) -> [U] { 6 | var transformedArray = [U]() 7 | for element in self { 8 | transformedArray.append(f(element)) 9 | } 10 | return transformedArray 11 | } 12 | } 13 | 14 | let numberOfCopies = [9, 12, 7, 15, 20] 15 | 16 | let isBiggerThanTen = numberOfCopies.transform{$0 > 10} 17 | 18 | isBiggerThanTen 19 | 20 | let revenue = numberOfCopies.transform{Double($0) * 0.99 * 0.70} 21 | 22 | revenue 23 | 24 | let isBiggerThanTenMap = numberOfCopies.map{$0 > 10} 25 | 26 | isBiggerThanTenMap 27 | 28 | let revenueMap = numberOfCopies.map{Double($0) * 0.99 * 0.70} 29 | 30 | revenueMap 31 | 32 | //: [TOC](TOC) | [Previous](@previous) | Next -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Custom Operators.xcplaygroundpage/Sources/Vertex.swift: -------------------------------------------------------------------------------- 1 | public func +(left: Vertex, right: Vertex) -> Vertex { 2 | return Vertex(x: left.x + right.x, y: left.y + right.y) 3 | } 4 | 5 | public struct Vertex { 6 | let x,y: Double 7 | 8 | public init(x: Double, y: Double) { 9 | self.x = x 10 | self.y = y 11 | } 12 | } 13 | 14 | extension Vertex: Movable { 15 | public var location: Vertex { 16 | return self 17 | } 18 | public func moveByX(deltaX: Double) -> Vertex { 19 | return Vertex(x: x + deltaX, y: y) 20 | } 21 | } 22 | 23 | extension Vertex: CustomStringConvertible { 24 | public var description: String { 25 | return "(\(x), \(y))" 26 | } 27 | } 28 | 29 | extension Vertex: Equatable{} 30 | public func ==(lhs: Vertex, rhs: Vertex) -> Bool { 31 | return lhs.x == rhs.x && lhs.y == rhs.y 32 | } -------------------------------------------------------------------------------- /4Collections.playground/Pages/Modify Arrays.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Modify Arrays 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | var coffeeDrinks = [String]() 5 | coffeeDrinks.count 6 | 7 | var drinkSizes = Array(count: 3, repeatedValue: "Really Big") 8 | drinkSizes[1] = "Big" 9 | drinkSizes.append("Huge") 10 | drinkSizes.insert("Moderate", atIndex: 1) 11 | drinkSizes[0 ... 1] = ["Medium"] 12 | drinkSizes[2 ... 2] = ["Bigger", "Jumbo"] 13 | drinkSizes += ["Huger"] 14 | drinkSizes = drinkSizes + ["Hugest"] 15 | drinkSizes.removeLast() 16 | drinkSizes.removeAtIndex(2) 17 | drinkSizes[2 ... 3] = [] 18 | drinkSizes.removeAll(keepCapacity: true) 19 | if !drinkSizes.isEmpty { 20 | drinkSizes.removeLast() 21 | } 22 | drinkSizes.count 23 | drinkSizes 24 | 25 | // causes a fatal error: drinkSizes[25] = "Out of range" 26 | 27 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 28 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Multiple Parameters.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Multiple Parameters 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | 5 | func hello() { 6 | hello("World") 7 | } 8 | func hello(name: String) { 9 | print("Hello, \(name)!") 10 | } 11 | func hello(name: String, numberOfTimes: Int) { 12 | for _ in 1 ... numberOfTimes { 13 | hello(name) 14 | } 15 | } 16 | func hello(name: String, numberOfTimes: Int, openParens: String, closeParens: String) { 17 | for i in 1 ... numberOfTimes { 18 | print(openParens, i, closeParens, separator:"", terminator: " ") 19 | hello(name) 20 | } 21 | } 22 | 23 | hello() 24 | hello("Swift Programmer") 25 | hello("Swift 2 Adopter", numberOfTimes: 3) 26 | hello("Programmer with Boundaries", numberOfTimes: 5, openParens: "<", closeParens: ">") 27 | 28 | 29 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 30 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Methods.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Methods 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | // mutating version 4 | //struct Vertex { 5 | // var x: Double 6 | // let y: Double 7 | // 8 | // mutating func moveByX(deltaX: Double) { 9 | // x = x + deltaX 10 | // } 11 | // 12 | //} 13 | 14 | struct Vertex { 15 | let x,y: Double 16 | 17 | func moveByX(deltaX: Double) -> Vertex { 18 | return Vertex(x: x + deltaX, y: y) 19 | } 20 | } 21 | 22 | var point = Vertex(x: 3.0, y: 4.0) 23 | point.moveByX(5) 24 | 25 | extension Vertex: CustomStringConvertible { 26 | var description: String { 27 | return "(\(x), \(y))" 28 | } 29 | } 30 | 31 | extension Vertex: Equatable{} 32 | func ==(lhs: Vertex, rhs: Vertex) -> Bool { 33 | return lhs.x == rhs.x && lhs.y == rhs.y 34 | } 35 | 36 | 37 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 38 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Custom Operators.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Custom Operators 2 | //: [TOC](TOC) | [Previous](@previous) | Next 3 | 4 | func shiftLeft(movable: T) -> T { 5 | return movable.moveByX(-1) 6 | } 7 | 8 | prefix operator « {} 9 | postfix operator » {} 10 | infix operator ∆ {} 11 | 12 | 13 | prefix func «(movable: T) -> T { 14 | return movable.moveByX(-1) 15 | } 16 | 17 | postfix func »(movable: T) -> T { 18 | return movable.moveByX(1) 19 | } 20 | 21 | func ∆(leftMovable: T, rightMovable: T) -> (shiftedLeft: T, shiftedRight: T) { 22 | return («leftMovable, rightMovable») 23 | } 24 | 25 | let point = Vertex(x: 3.0, y: 4.0) 26 | let shiftedLeftPoint = «point 27 | let shiftedRightPoint = point» 28 | let movedApartPoints = shiftedLeftPoint ∆ shiftedRightPoint 29 | let sum = shiftedLeftPoint + shiftedRightPoint 30 | 31 | //: [TOC](TOC) | [Previous](@previous) | Next 32 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Subclasses.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Subclasses 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | class ConferenceAttendee { 5 | let name, hometown: String 6 | 7 | init(name: String, hometown: String = "Cupertino") { 8 | self.name = name 9 | self.hometown = hometown 10 | } 11 | } 12 | 13 | class TutorialAttendee: ConferenceAttendee { 14 | let tutorial: String 15 | 16 | init(name: String, tutorial: String, hometown: String = "Cupertino" ) { 17 | self.tutorial = tutorial 18 | super.init(name: name, hometown: hometown) 19 | } 20 | } 21 | 22 | let daniel = ConferenceAttendee(name: "Daniel") 23 | let kimberli = TutorialAttendee(name: "Kimberli", tutorial: "Swift Kickstart") 24 | daniel.name 25 | daniel.hometown 26 | kimberli.name 27 | kimberli.hometown 28 | kimberli.tutorial 29 | 30 | 31 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 32 | -------------------------------------------------------------------------------- /2Functions.playground/Pages/Overloading.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Value and Reference Types.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Value and Reference Types 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | var x = 7 5 | var y = x 6 | x = 5 7 | print("x = \(x), y = \(y)") 8 | 9 | import UIKit 10 | 11 | var xField = UITextField() 12 | xField.text = "x" 13 | var yField = xField 14 | print("x = \(xField.text!), y = \(yField.text!)") 15 | yField.text = "y" 16 | print("x = \(xField.text!), y = \(yField.text!)") 17 | 18 | 19 | var xNumbers = [0, 1, 2, 3] 20 | var yNumbers = xNumbers 21 | xNumbers[0] = 100 22 | print("x = \(xNumbers), y = \(yNumbers)") 23 | 24 | var xFields = [xField, yField] 25 | var yFields = xFields 26 | var zField = UITextField() 27 | zField.text = "z" 28 | yFields[0] = zField 29 | print("x = \(xFields[0].text!), y = \(yFields[0].text!)") 30 | yFields[1].text = "Changed" 31 | print("x = \(xFields[1].text!), y = \(yFields[1].text!)") 32 | 33 | 34 | 35 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 36 | -------------------------------------------------------------------------------- /5FlexibleFunctions.playground/Pages/Closure Expressions.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Numerical Raw Values.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Numerical Raw Values 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | import UIKit 4 | 5 | enum Color: Int { 6 | case Yellow = 1 7 | case Green 8 | case Blue = 4 9 | case Purple 10 | 11 | var actualColor: UIColor { 12 | return UIColor(hue: hue, saturation: 1, brightness: 1, alpha: 1) 13 | } 14 | var hue: CGFloat { 15 | return CGFloat(rawValue) / 6 16 | } 17 | } 18 | 19 | let crayon = Color.Green 20 | crayon.rawValue 21 | crayon.actualColor 22 | 23 | enum Color2: CGFloat { 24 | case Yellow = 0.16667 25 | case Green = 0.33333 26 | case Blue = 0.666667 27 | case Purple = 0.833333 28 | 29 | var actualColor: UIColor { 30 | return UIColor(hue: rawValue, saturation: 1, brightness: 1, alpha: 1) 31 | } 32 | } 33 | 34 | let paintBrush = Color2.Purple 35 | paintBrush.actualColor 36 | 37 | 38 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 39 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Optionals and Dictionaries.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Optionals and Dictionaries 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let numbers = ["one":1, "two":2] 5 | let one = numbers["one"] 6 | let four = numbers["four"] 7 | 8 | if let validNumber = one { 9 | print("Number has value: \(validNumber)") 10 | } else { 11 | print("Number is nil") 12 | } 13 | 14 | func canUnwrap(potentialNumber: Int?) -> Bool { 15 | if let number = potentialNumber { 16 | print("Number has value: \(number)") 17 | return true 18 | } else { 19 | return false 20 | } 21 | } 22 | 23 | canUnwrap(one) 24 | canUnwrap(four) 25 | 26 | func guardedCanUnwrap(potentialNumber: Int?) -> Bool { 27 | guard let number = potentialNumber else { 28 | return false 29 | } 30 | print("Number has value: \(number)") 31 | return true 32 | } 33 | 34 | guardedCanUnwrap(one) 35 | guardedCanUnwrap(four) 36 | 37 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 38 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Methods.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Methods 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | import UIKit 5 | 6 | enum Color { 7 | case Red 8 | case Green 9 | case Blue 10 | 11 | func actualColor() -> UIColor { 12 | switch self { 13 | case .Red: 14 | return UIColor.redColor() 15 | case .Green: 16 | return UIColor.greenColor() 17 | case .Blue: 18 | return UIColor.blueColor() 19 | } 20 | } 21 | 22 | func colorSwatch(width width: Int, height: Int) -> UIView { 23 | let myView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height)) 24 | myView.backgroundColor = self.actualColor() 25 | return myView 26 | } 27 | } 28 | 29 | 30 | let crayon = Color.Green 31 | crayon.actualColor() 32 | crayon.colorSwatch(width: 200, height: 100) 33 | 34 | let paintBrush = Color.Blue 35 | paintBrush.actualColor() 36 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 37 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Computed Properties.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Computed Properties 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | import UIKit 5 | 6 | enum Color { 7 | case Red 8 | case Green 9 | case Blue 10 | 11 | var actualColor: UIColor { 12 | switch self { 13 | case .Red: 14 | return UIColor.redColor() 15 | case .Green: 16 | return UIColor.greenColor() 17 | case .Blue: 18 | return UIColor.blueColor() 19 | } 20 | } 21 | 22 | func colorSwatch(width width: Int, height: Int) -> UIView { 23 | let myView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: height)) 24 | myView.backgroundColor = self.actualColor 25 | return myView 26 | } 27 | } 28 | 29 | 30 | let crayon = Color.Green 31 | crayon.actualColor 32 | crayon.colorSwatch(width: 200, height: 100) 33 | 34 | let paintBrush = Color.Blue 35 | paintBrush.actualColor 36 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 37 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Type Alias.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Type Alias 2 | //: [TOC](TOC) | [Previous](@previous) | Next 3 | 4 | /// Used to measure payments in United States Dollars 5 | 6 | typealias USDollars = Double 7 | 8 | /// Used to represent the count of a discrete number of items 9 | 10 | typealias Count = Int 11 | 12 | /// A function type that calculates revenue for a number of copies. 13 | 14 | typealias RevenueCalculator = (Count) -> USDollars 15 | 16 | /** The return value is the revenue in US Dollars. */ 17 | 18 | func revenueForCopies(numberSold: Count) -> USDollars { 19 | return Double(numberSold * 99 * 70/100)/100 20 | } 21 | 22 | revenueForCopies(6) 23 | 24 | func totalRevenue(calculator: RevenueCalculator, copies: Count ...) -> USDollars { 25 | var runningTotal = 0.00 26 | for copy in copies { 27 | runningTotal += calculator(copy) 28 | } 29 | return runningTotal 30 | } 31 | 32 | totalRevenue(revenueForCopies, copies: 6, 3, 1) 33 | 34 | //: [TOC](TOC) | [Previous](@previous) | Next 35 | -------------------------------------------------------------------------------- /4Collections.playground/Pages/Create Arrays.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Create Arrays 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let people = ["Swift Programmer", "Obj-C Old-timer"] 5 | 6 | let morePeople : [String] 7 | 8 | morePeople = ["Swift 2 Adopter", "Protocol Pro"] 9 | 10 | func createGreetingWithSalutation(salutation: String) -> (String) -> String { 11 | func greetingFunction(name: String) -> String { 12 | let greeting = "\(salutation), \(name)!" 13 | return greeting 14 | } 15 | return greetingFunction 16 | } 17 | 18 | let greetings = [createGreetingWithSalutation("Hello"), 19 | createGreetingWithSalutation("Bonjour"), 20 | createGreetingWithSalutation("Hey")] 21 | 22 | let helloSwiftProgrammer = greetings[0](people[0]) 23 | 24 | let salutations = ["Hello", "Bonjour", "Hey"] 25 | 26 | let simplerGreetings = salutations.map(createGreetingWithSalutation) 27 | 28 | let heyProtocolPro = simplerGreetings[2](morePeople[1]) 29 | 30 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 31 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Composition.xcplaygroundpage/Sources/Rectangle.swift: -------------------------------------------------------------------------------- 1 | public struct Rectangle { 2 | let topLeftCorner: Vertex 3 | let size: Size 4 | 5 | public init(topLeftCorner: Vertex, width: Double, height: Double) { 6 | self.topLeftCorner = topLeftCorner 7 | self.size = Size(width: width, height: height) 8 | } 9 | 10 | private init(topLeftCorner: Vertex, size: Size) { 11 | self.topLeftCorner = topLeftCorner 12 | self.size = size 13 | } 14 | 15 | public func moveByX(deltaX: Double) -> Rectangle { 16 | let movedTopLeftCorner = topLeftCorner.moveByX(deltaX) 17 | return Rectangle(topLeftCorner: movedTopLeftCorner, size: size) 18 | } 19 | } 20 | 21 | extension Rectangle: CustomStringConvertible { 22 | public var description: String { 23 | return "\(size) at \(topLeftCorner)" 24 | } 25 | } 26 | 27 | extension Rectangle: Equatable{} 28 | public func ==(lhs: Rectangle, rhs: Rectangle) -> Bool { 29 | return lhs.size == rhs.size && lhs.topLeftCorner == rhs.topLeftCorner 30 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Creating a Protocol.xcplaygroundpage/Sources/Rectangle.swift: -------------------------------------------------------------------------------- 1 | public struct Rectangle { 2 | let topLeftCorner: Vertex 3 | let size: Size 4 | 5 | public init(topLeftCorner: Vertex, width: Double, height: Double) { 6 | self.topLeftCorner = topLeftCorner 7 | self.size = Size(width: width, height: height) 8 | } 9 | 10 | private init(topLeftCorner: Vertex, size: Size) { 11 | self.topLeftCorner = topLeftCorner 12 | self.size = size 13 | } 14 | 15 | public func moveByX(deltaX: Double) -> Rectangle { 16 | let movedTopLeftCorner = topLeftCorner.moveByX(deltaX) 17 | return Rectangle(topLeftCorner: movedTopLeftCorner, size: size) 18 | } 19 | } 20 | 21 | extension Rectangle: CustomStringConvertible { 22 | public var description: String { 23 | return "\(size) at \(topLeftCorner)" 24 | } 25 | } 26 | 27 | extension Rectangle: Equatable{} 28 | public func ==(lhs: Rectangle, rhs: Rectangle) -> Bool { 29 | return lhs.size == rhs.size && lhs.topLeftCorner == rhs.topLeftCorner 30 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Self.xcplaygroundpage/Sources/Rectangle.swift: -------------------------------------------------------------------------------- 1 | public struct Rectangle { 2 | let topLeftCorner: Vertex 3 | let size: Size 4 | 5 | public init(topLeftCorner: Vertex, width: Double, height: Double) { 6 | self.topLeftCorner = topLeftCorner 7 | self.size = Size(width: width, height: height) 8 | } 9 | } 10 | 11 | extension Rectangle: Movable { 12 | private init(topLeftCorner: Vertex, size: Size) { 13 | self.topLeftCorner = topLeftCorner 14 | self.size = size 15 | } 16 | public func moveByX(deltaX: Double) -> Rectangle { 17 | let movedTopLeftCorner = topLeftCorner.moveByX(deltaX) 18 | return Rectangle(topLeftCorner: movedTopLeftCorner, size: size) 19 | } 20 | } 21 | 22 | extension Rectangle: CustomStringConvertible { 23 | public var description: String { 24 | return "\(size) at \(topLeftCorner)" 25 | } 26 | } 27 | 28 | extension Rectangle: Equatable{} 29 | public func ==(lhs: Rectangle, rhs: Rectangle) -> Bool { 30 | return lhs.size == rhs.size && lhs.topLeftCorner == rhs.topLeftCorner 31 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Casting.xcplaygroundpage/Sources/Rectangle.swift: -------------------------------------------------------------------------------- 1 | public struct Rectangle { 2 | let topLeftCorner: Vertex 3 | let size: Size 4 | 5 | public init(topLeftCorner: Vertex, width: Double, height: Double) { 6 | self.topLeftCorner = topLeftCorner 7 | self.size = Size(width: width, height: height) 8 | } 9 | 10 | private init(topLeftCorner: Vertex, size: Size) { 11 | self.topLeftCorner = topLeftCorner 12 | self.size = size 13 | } 14 | 15 | public func moveByX(deltaX: Double) -> Rectangle { 16 | guard let movedTopLeftCorner = topLeftCorner.moveByX(deltaX) as? Vertex else { 17 | return self 18 | } 19 | return Rectangle(topLeftCorner: movedTopLeftCorner, size: size) 20 | } 21 | } 22 | 23 | extension Rectangle: CustomStringConvertible { 24 | public var description: String { 25 | return "\(size) at \(topLeftCorner)" 26 | } 27 | } 28 | 29 | extension Rectangle: Equatable{} 30 | public func ==(lhs: Rectangle, rhs: Rectangle) -> Bool { 31 | return lhs.size == rhs.size && lhs.topLeftCorner == rhs.topLeftCorner 32 | } -------------------------------------------------------------------------------- /9Classes.playground/Pages/Composition.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Composition 2 | //: [TOC](TOC) | [Previous](@previous) | Next 3 | 4 | protocol Namable: CustomStringConvertible { 5 | func nameBadge() -> String 6 | } 7 | 8 | extension Namable { 9 | var description: String { 10 | return nameBadge() 11 | } 12 | } 13 | 14 | struct ConferenceAttendee { 15 | let name: String 16 | 17 | } 18 | 19 | extension ConferenceAttendee: Namable { 20 | func nameBadge() -> String { 21 | return "Hi, I'm \(name)." 22 | } 23 | } 24 | 25 | struct TutorialAttendee { 26 | let attendee: ConferenceAttendee 27 | let tutorial: String 28 | var name: String { 29 | return attendee.name 30 | } 31 | } 32 | 33 | extension TutorialAttendee: Namable { 34 | func nameBadge() -> String { 35 | return attendee.nameBadge() + " I'm taking \(tutorial)." 36 | } 37 | } 38 | 39 | let daniel = ConferenceAttendee(name: "Daniel") 40 | let kimberli = TutorialAttendee(attendee: ConferenceAttendee(name: "Kimberli"), tutorial: "Swift Kickstart") 41 | 42 | 43 | //: [TOC](TOC) | [Previous](@previous) | Next 44 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Composition Preparation.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Composition Preparation 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | //: This is the "before" picture. The "after" picture is on the next page. 5 | 6 | class ConferenceAttendee { 7 | let name: String 8 | 9 | init(name: String) { 10 | self.name = name 11 | } 12 | func nameBadge() -> String { 13 | return "Hi, I'm \(name)." 14 | } 15 | } 16 | extension ConferenceAttendee: CustomStringConvertible { 17 | var description: String { 18 | return nameBadge() 19 | } 20 | } 21 | class TutorialAttendee: ConferenceAttendee { 22 | let tutorial: String 23 | init(name: String, tutorial: String){ 24 | self.tutorial = tutorial 25 | super.init(name: name) 26 | } 27 | override func nameBadge() -> String { 28 | return super.nameBadge() + " I'm taking \(tutorial)." 29 | } 30 | } 31 | 32 | let daniel = ConferenceAttendee(name: "Daniel") 33 | let kimberli = TutorialAttendee(name: "Kimberli", tutorial: "Swift Kickstart") 34 | 35 | 36 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 37 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Conforming to a Protocol.xcplaygroundpage/Sources/Rectangle.swift: -------------------------------------------------------------------------------- 1 | public struct Rectangle { 2 | let topLeftCorner: Vertex 3 | let size: Size 4 | 5 | public init(topLeftCorner: Vertex, width: Double, height: Double) { 6 | self.topLeftCorner = topLeftCorner 7 | self.size = Size(width: width, height: height) 8 | } 9 | 10 | private init(topLeftCorner: Vertex, size: Size) { 11 | self.topLeftCorner = topLeftCorner 12 | self.size = size 13 | } 14 | // there is an error in this method that we'll fix commenting it out for now 15 | /* public func moveByX(deltaX: Double) -> Rectangle { 16 | let movedTopLeftCorner = topLeftCorner.moveByX(deltaX) 17 | return Rectangle(topLeftCorner: movedTopLeftCorner, size: size) 18 | } */ 19 | } 20 | 21 | extension Rectangle: CustomStringConvertible { 22 | public var description: String { 23 | return "\(size) at \(topLeftCorner)" 24 | } 25 | } 26 | 27 | extension Rectangle: Equatable{} 28 | public func ==(lhs: Rectangle, rhs: Rectangle) -> Bool { 29 | return lhs.size == rhs.size && lhs.topLeftCorner == rhs.topLeftCorner 30 | } -------------------------------------------------------------------------------- /9Classes.playground/Pages/Methods.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Methods 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | class ConferenceAttendee { 5 | let name, hometown: String 6 | 7 | init(name: String, hometown: String = "Cupertino") { 8 | self.name = name 9 | self.hometown = hometown 10 | } 11 | func nameBadge() -> String { 12 | return "Hi, I'm \(name) from \(hometown)." 13 | } 14 | } 15 | 16 | extension ConferenceAttendee: CustomStringConvertible { 17 | var description: String { 18 | return nameBadge() 19 | } 20 | } 21 | 22 | class TutorialAttendee: ConferenceAttendee { 23 | let tutorial: String 24 | 25 | init(name: String, tutorial: String, hometown: String = "Cupertino" ) { 26 | self.tutorial = tutorial 27 | super.init(name: name, hometown: hometown) 28 | } 29 | override func nameBadge() -> String { 30 | return super.nameBadge() + " I'm taking \(tutorial)." 31 | } 32 | } 33 | 34 | let daniel = ConferenceAttendee(name: "Daniel") 35 | let kimberli = TutorialAttendee(name: "Kimberli", tutorial: "Swift Kickstart") 36 | daniel.nameBadge() 37 | kimberli 38 | 39 | 40 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 41 | -------------------------------------------------------------------------------- /7Structs.playground/Pages/Will Set Did Set.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Will Set Did Set 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | import Foundation 4 | 5 | struct Vertex { 6 | private(set) var x: Double { 7 | willSet { 8 | print("The value of x is changing from \(self.x) to \(newValue).") 9 | } 10 | didSet { 11 | if x == 6.0 { 12 | x = -6.0 13 | print("The proposed new value of x was 6, we're changing it to -6.") 14 | } else { 15 | print("The value of x has changed from \(oldValue) to \(x).") 16 | 17 | } 18 | } 19 | } 20 | private(set) var y: Double 21 | 22 | var magnitude: Double { 23 | get { 24 | return sqrt(x * x + y * y) 25 | } 26 | set { 27 | let multiplier = newValue / magnitude 28 | x *= multiplier 29 | y *= multiplier 30 | } 31 | } 32 | 33 | init(x: Double = 3.0, y: Double = 4.0) { 34 | self.x = x 35 | self.y = y 36 | } 37 | 38 | } 39 | 40 | var pointThreeFour = Vertex() 41 | pointThreeFour.magnitude 42 | pointThreeFour.magnitude = 10.0 43 | pointThreeFour 44 | 45 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 46 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Generics.xcplaygroundpage/Sources/Rectangle.swift: -------------------------------------------------------------------------------- 1 | public struct Rectangle { 2 | let topLeftCorner: Vertex 3 | let size: Size 4 | public let location: Vertex 5 | 6 | public init(topLeftCorner: Vertex, width: Double, height: Double) { 7 | self.topLeftCorner = topLeftCorner 8 | self.size = Size(width: width, height: height) 9 | location = Vertex(x: topLeftCorner.x + size.width/2, y: topLeftCorner.y + size.height/2) 10 | } 11 | } 12 | 13 | extension Rectangle: Movable { 14 | private init(topLeftCorner: Vertex, size: Size) { 15 | self.topLeftCorner = topLeftCorner 16 | self.size = size 17 | location = Vertex(x: topLeftCorner.x + size.width/2, y: topLeftCorner.y + size.height/2) 18 | } 19 | public func moveByX(deltaX: Double) -> Rectangle { 20 | let movedTopLeftCorner = topLeftCorner.moveByX(deltaX) 21 | return Rectangle(topLeftCorner: movedTopLeftCorner, size: size) 22 | } 23 | } 24 | 25 | extension Rectangle: CustomStringConvertible { 26 | public var description: String { 27 | return "\(size) at \(topLeftCorner)" 28 | } 29 | } 30 | 31 | extension Rectangle: Equatable{} 32 | public func ==(lhs: Rectangle, rhs: Rectangle) -> Bool { 33 | return lhs.size == rhs.size && lhs.topLeftCorner == rhs.topLeftCorner 34 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Properties.xcplaygroundpage/Sources/Rectangle.swift: -------------------------------------------------------------------------------- 1 | public struct Rectangle { 2 | let topLeftCorner: Vertex 3 | let size: Size 4 | public let location: Vertex 5 | 6 | public init(topLeftCorner: Vertex, width: Double, height: Double) { 7 | self.topLeftCorner = topLeftCorner 8 | self.size = Size(width: width, height: height) 9 | location = Vertex(x: topLeftCorner.x + size.width/2, y: topLeftCorner.y + size.height/2) 10 | } 11 | } 12 | 13 | extension Rectangle: Movable { 14 | private init(topLeftCorner: Vertex, size: Size) { 15 | self.topLeftCorner = topLeftCorner 16 | self.size = size 17 | location = Vertex(x: topLeftCorner.x + size.width/2, y: topLeftCorner.y + size.height/2) 18 | } 19 | public func moveByX(deltaX: Double) -> Rectangle { 20 | let movedTopLeftCorner = topLeftCorner.moveByX(deltaX) 21 | return Rectangle(topLeftCorner: movedTopLeftCorner, size: size) 22 | } 23 | } 24 | 25 | extension Rectangle: CustomStringConvertible { 26 | public var description: String { 27 | return "\(size) at \(topLeftCorner)" 28 | } 29 | } 30 | 31 | extension Rectangle: Equatable{} 32 | public func ==(lhs: Rectangle, rhs: Rectangle) -> Bool { 33 | return lhs.size == rhs.size && lhs.topLeftCorner == rhs.topLeftCorner 34 | } -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Polymorphism.xcplaygroundpage/Sources/Rectangle.swift: -------------------------------------------------------------------------------- 1 | public struct Rectangle { 2 | let topLeftCorner: Vertex 3 | let size: Size 4 | public let location: Vertex 5 | 6 | public init(topLeftCorner: Vertex, width: Double, height: Double) { 7 | self.topLeftCorner = topLeftCorner 8 | self.size = Size(width: width, height: height) 9 | location = Vertex(x: topLeftCorner.x + size.width/2, y: topLeftCorner.y + size.height/2) 10 | } 11 | } 12 | 13 | extension Rectangle: Movable { 14 | private init(topLeftCorner: Vertex, size: Size) { 15 | self.topLeftCorner = topLeftCorner 16 | self.size = size 17 | location = Vertex(x: topLeftCorner.x + size.width/2, y: topLeftCorner.y + size.height/2) 18 | } 19 | public func moveByX(deltaX: Double) -> Rectangle { 20 | let movedTopLeftCorner = topLeftCorner.moveByX(deltaX) 21 | return Rectangle(topLeftCorner: movedTopLeftCorner, size: size) 22 | } 23 | } 24 | 25 | extension Rectangle: CustomStringConvertible { 26 | public var description: String { 27 | return "\(size) at \(topLeftCorner)" 28 | } 29 | } 30 | 31 | extension Rectangle: Equatable{} 32 | public func ==(lhs: Rectangle, rhs: Rectangle) -> Bool { 33 | return lhs.size == rhs.size && lhs.topLeftCorner == rhs.topLeftCorner 34 | } -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Nil.xcplaygroundpage/timeline.xctimeline: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Custom Operators.xcplaygroundpage/Sources/Rectangle.swift: -------------------------------------------------------------------------------- 1 | public struct Rectangle { 2 | let topLeftCorner: Vertex 3 | let size: Size 4 | public let location: Vertex 5 | 6 | public init(topLeftCorner: Vertex, width: Double, height: Double) { 7 | self.topLeftCorner = topLeftCorner 8 | self.size = Size(width: width, height: height) 9 | location = Vertex(x: topLeftCorner.x + size.width/2, y: topLeftCorner.y + size.height/2) 10 | } 11 | } 12 | 13 | extension Rectangle: Movable { 14 | private init(topLeftCorner: Vertex, size: Size) { 15 | self.topLeftCorner = topLeftCorner 16 | self.size = size 17 | location = Vertex(x: topLeftCorner.x + size.width/2, y: topLeftCorner.y + size.height/2) 18 | } 19 | public func moveByX(deltaX: Double) -> Rectangle { 20 | let movedTopLeftCorner = topLeftCorner.moveByX(deltaX) 21 | return Rectangle(topLeftCorner: movedTopLeftCorner, size: size) 22 | } 23 | public func whereAmI() -> Vertex { 24 | return topLeftCorner 25 | } 26 | } 27 | 28 | extension Rectangle: CustomStringConvertible { 29 | public var description: String { 30 | return "\(size) at \(topLeftCorner)" 31 | } 32 | } 33 | 34 | extension Rectangle: Equatable{} 35 | public func ==(lhs: Rectangle, rhs: Rectangle) -> Bool { 36 | return lhs.size == rhs.size && lhs.topLeftCorner == rhs.topLeftCorner 37 | } -------------------------------------------------------------------------------- /9Classes.playground/Pages/Optional Property Values.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Optional Property Values 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | class ConferenceAttendee { 5 | let name: String 6 | let hometown: String? 7 | 8 | init(name: String, hometown: String? = nil) { 9 | self.name = name 10 | self.hometown = hometown 11 | } 12 | func nameBadge() -> String { 13 | let greeting = "Hi, I'm \(name)" 14 | guard let validHometown = hometown else { 15 | return greeting + "." 16 | } 17 | return greeting + " from \(validHometown)." 18 | } 19 | } 20 | 21 | extension ConferenceAttendee: CustomStringConvertible { 22 | var description: String { 23 | return nameBadge() 24 | } 25 | } 26 | 27 | class TutorialAttendee: ConferenceAttendee { 28 | let tutorial: String 29 | 30 | init(name: String, tutorial: String, hometown: String? = nil ) { 31 | self.tutorial = tutorial 32 | super.init(name: name, hometown: hometown) 33 | } 34 | override func nameBadge() -> String { 35 | return super.nameBadge() + " I'm taking \(tutorial)." 36 | } 37 | } 38 | 39 | let daniel = ConferenceAttendee(name: "Daniel") 40 | let kimberli = TutorialAttendee(name: "Kimberli", tutorial: "Swift Kickstart") 41 | daniel 42 | kimberli 43 | 44 | 45 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 46 | -------------------------------------------------------------------------------- /8Protocols.playground/Pages/Protocol Extensions.xcplaygroundpage/Sources/Rectangle.swift: -------------------------------------------------------------------------------- 1 | public struct Rectangle { 2 | let topLeftCorner: Vertex 3 | let size: Size 4 | public let location: Vertex 5 | 6 | public init(topLeftCorner: Vertex, width: Double, height: Double) { 7 | self.topLeftCorner = topLeftCorner 8 | self.size = Size(width: width, height: height) 9 | location = Vertex(x: topLeftCorner.x + size.width/2, y: topLeftCorner.y + size.height/2) 10 | } 11 | } 12 | 13 | extension Rectangle: Movable { 14 | private init(topLeftCorner: Vertex, size: Size) { 15 | self.topLeftCorner = topLeftCorner 16 | self.size = size 17 | location = Vertex(x: topLeftCorner.x + size.width/2, y: topLeftCorner.y + size.height/2) 18 | } 19 | public func moveByX(deltaX: Double) -> Rectangle { 20 | let movedTopLeftCorner = topLeftCorner.moveByX(deltaX) 21 | return Rectangle(topLeftCorner: movedTopLeftCorner, size: size) 22 | } 23 | public func whereAmI() -> Vertex { 24 | return topLeftCorner 25 | } 26 | } 27 | 28 | extension Rectangle: CustomStringConvertible { 29 | public var description: String { 30 | return "\(size) at \(topLeftCorner)" 31 | } 32 | } 33 | 34 | extension Rectangle: Equatable{} 35 | public func ==(lhs: Rectangle, rhs: Rectangle) -> Bool { 36 | return lhs.size == rhs.size && lhs.topLeftCorner == rhs.topLeftCorner 37 | } -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Error Type.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Error Type 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | enum InputSizeError: ErrorType { 5 | case NegativeNumberError 6 | case NumberIsTooLargeError(amountOver: Int) 7 | } 8 | 9 | func double(input: Int) throws -> Int { 10 | if input < 0 { 11 | throw InputSizeError.NegativeNumberError 12 | } else if input > 49 { 13 | throw InputSizeError.NumberIsTooLargeError(amountOver: input - 49) 14 | } else { 15 | return input * 2 16 | } 17 | } 18 | 19 | 20 | func cautiousDoubler(input: Int) { 21 | do { 22 | print("double", input, "=", try double(input)) 23 | } 24 | catch InputSizeError.NumberIsTooLargeError(let excess) { 25 | print("Sorry, input is too big by", excess) 26 | } 27 | catch { 28 | print("Error:", error) 29 | } 30 | } 31 | 32 | cautiousDoubler(8) 33 | cautiousDoubler(-8) 34 | cautiousDoubler(80) 35 | 36 | let ten = try! double(5) 37 | // this is a runtime crash let negativeTwo = try! double(-1) 38 | 39 | let six = try? double(3) 40 | let negativeTwo = try? double(-1) 41 | let twoHundred = try? double(100) 42 | 43 | let ints = [6, -1, 12, 100, 20] 44 | let doubleInts = ints.flatMap{element in try? double(element)} 45 | doubleInts 46 | let altDoubleInts = ints.flatMap{ try? double($0)} 47 | altDoubleInts//: [TOC](TOC) | [Previous](@previous) | [Next](@next) 48 | -------------------------------------------------------------------------------- /3VariablesAndConstants.playground/Pages/Higher Order Functions.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Higher Order Functions 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | let friend = "Daniel" 5 | 6 | func hello(name: String) -> String { 7 | let greeting = "Hello, \(name)!" 8 | return greeting 9 | } 10 | 11 | hello(friend) 12 | 13 | func bonjour(name: String) -> String { 14 | let greeting = "Bonjour, \(name)!" 15 | return greeting 16 | } 17 | 18 | bonjour(friend) 19 | 20 | func greet(name: String, withSalutation salutation: String) -> String { 21 | let greeting = "\(salutation), \(name)!" 22 | return greeting 23 | } 24 | 25 | greet(friend, withSalutation: "Hi there") 26 | 27 | func createGreetingWithSalutation(salutation: String) -> (String) -> String { 28 | func greetingFunction(name: String) -> String { 29 | let greeting = "\(salutation), \(name)!" 30 | return greeting 31 | } 32 | return greetingFunction 33 | } 34 | 35 | let heyThere = createGreetingWithSalutation("Hey there") 36 | heyThere(friend) 37 | 38 | createGreetingWithSalutation("Hi")(friend) 39 | 40 | func useGreeting(greetingFor:(String) -> (String), forPeopleWithName people: String...) -> String { 41 | var tempGreeting = "" 42 | for person in people { 43 | tempGreeting += greetingFor(person) + "\n" 44 | } 45 | return tempGreeting 46 | } 47 | 48 | useGreeting(heyThere, forPeopleWithName: "Swift Programmer", "Swift 2 Adopter") 49 | 50 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 51 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Failable Initializers.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Failable Initializers 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | class ConferenceAttendee { 5 | let name: String 6 | let hometown: String? 7 | 8 | init?(name: String, hometown: String? = nil) { 9 | self.name = name 10 | self.hometown = hometown 11 | if hometown == nil { 12 | return nil 13 | } 14 | } 15 | func nameBadge() -> String { 16 | let greeting = "Hi, I'm \(name)" 17 | guard let validHometown = hometown else { 18 | return greeting + "." 19 | } 20 | return greeting + " from \(validHometown)." 21 | } 22 | } 23 | 24 | extension ConferenceAttendee: CustomStringConvertible { 25 | var description: String { 26 | return nameBadge() 27 | } 28 | } 29 | 30 | class TutorialAttendee: ConferenceAttendee { 31 | let tutorial: String 32 | 33 | 34 | 35 | init(name: String, tutorial: String, hometown: String ) { 36 | self.tutorial = tutorial 37 | super.init(name: name, hometown: hometown)! 38 | } 39 | override func nameBadge() -> String { 40 | return super.nameBadge() + " I'm taking \(tutorial)." 41 | } 42 | } 43 | 44 | let daniel = ConferenceAttendee(name: "Daniel") 45 | let maggie = ConferenceAttendee(name: "Maggie", hometown: "Cleveland") 46 | let kimberli = TutorialAttendee(name: "Kimberli", tutorial: "Swift Kickstart", hometown: "Shaker Heights") 47 | 48 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 49 | -------------------------------------------------------------------------------- /6Enumerations.playground/Pages/Binding to Associated Values.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Binding to Associated Values 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | enum PrimaryColor: String { 5 | case Red 6 | case Yellow 7 | case Blue 8 | } 9 | 10 | enum Desktop { 11 | case Black 12 | case White 13 | case Color(PrimaryColor) 14 | case Tiled(PrimaryColor, PrimaryColor, Int, Int) 15 | } 16 | 17 | let backgrounds = [Desktop.Black, Desktop.Color(.Red), Desktop.White, Desktop.Color(.Blue)] 18 | 19 | for background in backgrounds { 20 | switch background { 21 | case .Color(let primaryColor): 22 | print("This background color is \(primaryColor.rawValue).") 23 | default: 24 | print("This background has no color.") 25 | } 26 | } 27 | print("") 28 | 29 | for background in backgrounds { 30 | if case .Color(let primaryColor) = background where primaryColor == .Red { 31 | print("This background is Red.") 32 | } else { 33 | print("This background is not Red.") 34 | } 35 | } 36 | 37 | print("") 38 | 39 | for case .Color(let primaryColor) in backgrounds where primaryColor != .Red { 40 | print("This background is \(primaryColor.rawValue). It isn't .Red.") 41 | } 42 | 43 | let tiledBackground = Desktop.Tiled(.Red, .Blue, 5, 7) 44 | 45 | switch tiledBackground { 46 | case let .Tiled(firstColor, secondColor, numberOfRows, numberOfColumns): 47 | print("The background is tiled using colors \(firstColor.rawValue) and \(secondColor.rawValue) with \(numberOfRows) rows and \(numberOfColumns) columns.") 48 | default: 49 | print("The background is not tiled.") 50 | } 51 | 52 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 53 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Required Initializers.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Required Initializers 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | class ConferenceAttendee { 5 | let name: String 6 | let hometown: String? 7 | 8 | required init?(name: String, hometown: String? = nil) { 9 | self.name = name 10 | self.hometown = hometown 11 | if hometown == nil { 12 | return nil 13 | } 14 | } 15 | func nameBadge() -> String { 16 | let greeting = "Hi, I'm \(name)" 17 | guard let validHometown = hometown else { 18 | return greeting + "." 19 | } 20 | return greeting + " from \(validHometown)." 21 | } 22 | } 23 | 24 | extension ConferenceAttendee: CustomStringConvertible { 25 | var description: String { 26 | return nameBadge() 27 | } 28 | } 29 | 30 | class TutorialAttendee: ConferenceAttendee { 31 | let tutorial: String 32 | 33 | 34 | 35 | init(name: String, tutorial: String, hometown: String ) { 36 | self.tutorial = tutorial 37 | super.init(name: name, hometown: hometown)! 38 | } 39 | 40 | // required init?(name: String, hometown: String?) { 41 | // self.tutorial = "No tutorial set" 42 | // super.init(name: name, hometown: hometown) 43 | // return nil 44 | // } 45 | 46 | required init(name: String, hometown: String? = nil) { 47 | self.tutorial = "no tutorial at all" 48 | super.init(name: name, hometown: "this hotel")! 49 | } 50 | override func nameBadge() -> String { 51 | return super.nameBadge() + " I'm taking \(tutorial)." 52 | } 53 | } 54 | 55 | let daniel = ConferenceAttendee(name: "Daniel") 56 | let maggie = ConferenceAttendee(name: "Maggie", hometown: "Cleveland") 57 | let kimberli = TutorialAttendee(name: "Kimberli", tutorial: "Swift Kickstart", hometown: "Shaker Heights") 58 | let tara = TutorialAttendee(name: "Tara") 59 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 60 | -------------------------------------------------------------------------------- /9Classes.playground/Pages/Optional Chaining.xcplaygroundpage/Contents.swift: -------------------------------------------------------------------------------- 1 | //: ### Optional Chaining 2 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 3 | 4 | class StreetInformation { 5 | let streetNumberAndName: String? 6 | init(streetNumberAndName: String? = nil){ 7 | self.streetNumberAndName = streetNumberAndName 8 | } 9 | } 10 | 11 | class Address { 12 | let streetInformation: StreetInformation? 13 | let city: String 14 | let state: String 15 | 16 | init(city: String, state: String, streetInformation: StreetInformation? = nil){ 17 | self.city = city 18 | self.state = state 19 | self.streetInformation = streetInformation 20 | } 21 | } 22 | 23 | 24 | class ConferenceAttendee { 25 | let name: String 26 | let address: Address? 27 | 28 | init(name: String, address: Address? = nil) { 29 | self.name = name 30 | self.address = address 31 | } 32 | } 33 | 34 | let daniel = ConferenceAttendee(name: "Daniel") 35 | let kimberli = ConferenceAttendee(name: "Kimberli", address: Address(city: "Cleveland", state: "OH", streetInformation: StreetInformation(streetNumberAndName: "1234 Elm Street"))) 36 | 37 | typealias StreetNumberAndName = String 38 | 39 | func pyramidOfDoom(attendee: ConferenceAttendee) -> StreetNumberAndName { 40 | if let validAddress = attendee.address { 41 | if let validStreetInformation = validAddress.streetInformation { 42 | if let validStreetNumberAndName = validStreetInformation.streetNumberAndName { 43 | return validStreetNumberAndName 44 | } 45 | } 46 | } 47 | return "No street number and name available" 48 | } 49 | 50 | pyramidOfDoom(daniel) 51 | pyramidOfDoom(kimberli) 52 | 53 | func flattenedPyramid(attendee: ConferenceAttendee) -> StreetNumberAndName { 54 | if let validAddress = attendee.address, validStreetInformation = validAddress.streetInformation, validStreetNumberAndName = validStreetInformation.streetNumberAndName { 55 | return validStreetNumberAndName 56 | } 57 | return "No street number and name available" 58 | } 59 | 60 | flattenedPyramid(daniel) 61 | flattenedPyramid(kimberli) 62 | 63 | func guardVersion(attendee: ConferenceAttendee) -> StreetNumberAndName { 64 | guard let validAddress = attendee.address, validStreetInformation = validAddress.streetInformation, validStreetNumberAndName = validStreetInformation.streetNumberAndName else { 65 | return "No street number and name available" 66 | } 67 | return validStreetNumberAndName 68 | } 69 | guardVersion(daniel) 70 | guardVersion(kimberli) 71 | 72 | func optionalChainingVersion(attendee: ConferenceAttendee) -> StreetNumberAndName { 73 | return attendee.address?.streetInformation?.streetNumberAndName ?? "No street number and name available" 74 | } 75 | 76 | optionalChainingVersion(daniel) 77 | optionalChainingVersion(kimberli) 78 | 79 | //: [TOC](TOC) | [Previous](@previous) | [Next](@next) 80 | --------------------------------------------------------------------------------