Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #20

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion 10. Properties.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
1 change: 1 addition & 0 deletions 10. Properties.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ------------------------------------------------------------------------------------------------
// Checked September 2016
// Things to know:
//
// * Properties store values in classes, structures and enumerations.
Expand Down
4 changes: 3 additions & 1 deletion 10. Properties.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=10176&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=429121248.87547">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=10202&amp;EndingColumnNumber=5&amp;EndingLineNumber=3&amp;StartingColumnNumber=4&amp;StartingLineNumber=3&amp;Timestamp=496707723.358648"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
2 changes: 1 addition & 1 deletion 11. Methods.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
31 changes: 16 additions & 15 deletions 11. Methods.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ------------------------------------------------------------------------------------------------
// Checked and updated September 2016
// Things to know:
//
// * Methods can be in the form of Instance Methods, which apply to a given instance of a class
Expand Down Expand Up @@ -45,7 +46,7 @@ class Counter
// No parameters
func increment()
{
count++
count += 1
}

// One parameter, no external parameter name needed by caller
Expand Down Expand Up @@ -80,7 +81,7 @@ class Counter
// Two parameters. Using the external parameter shorthand ("#") to force caller to use
// external parameter name on first parameter and defaulting to shared local/external names
// for the rest.
func addTwiceWithExternalSpecified2(#first: Int, second: Int)
func addTwiceWithExternalSpecified2(first: Int, second: Int)
{
count += first
count += second
Expand All @@ -97,12 +98,12 @@ class Counter
// Now let's see how we call each of those functions
var counter = Counter()
counter.increment()
counter.incrementBy(4)
counter.incrementBy(amount: 4)
counter.addValueTo(value: 4)
counter.addTwiceWithExternalImplied(50, second: 4)
counter.addTwiceWithExternalImplied(first: 50, second: 4)
counter.addTwiceWithExternalSpecified(a: 50, b: 4)
counter.addTwiceWithExternalSpecified2(first: 10, second: 10)
counter.addTwiceWithExternalSpecified3(10, 10)
counter.addTwiceWithExternalSpecified3(first: 10, 10)
counter.count

// The 'self' property refers to the current instance of a class, structure or enumeration. For
Expand Down Expand Up @@ -160,17 +161,17 @@ struct Point3
// enumeration:
enum TriStateSwitch
{
case Off, Low, High
case off, low, high
mutating func next()
{
switch self
{
case Off:
self = Low
case Low:
self = High
case High:
self = Off
case .off:
self = .low
case .low:
self = .high
case .high:
self = .off
}
}
}
Expand Down Expand Up @@ -199,7 +200,7 @@ struct LevelTracker
}
mutating func advanceToLevel(level: Int) -> Bool
{
if LevelTracker.levelIsUnlocked(level)
if LevelTracker.levelIsUnlocked(level: level)
{
currentLevel = level
return true
Expand All @@ -212,7 +213,7 @@ struct LevelTracker
}

// To call a type method, use the type name, not the instance name:
LevelTracker.levelIsUnlocked(3)
LevelTracker.levelIsUnlocked(level: 3)

// If we attempt to use an instance to call a type method, we'll get an error
var levelTracker = LevelTracker()
Expand All @@ -231,4 +232,4 @@ class SomeOtherClass
}

// We call class type methods with the type name just as we do for structures and enumerations:
SomeOtherClass.isGreaterThan100(105)
SomeOtherClass.isGreaterThan100(value: 105)
4 changes: 3 additions & 1 deletion 11. Methods.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=6306&amp;EndingColumnNumber=5&amp;EndingLineNumber=8&amp;StartingColumnNumber=4&amp;StartingLineNumber=8&amp;Timestamp=424368474.022727">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=6395&amp;EndingColumnNumber=5&amp;EndingLineNumber=9&amp;StartingColumnNumber=4&amp;StartingLineNumber=9&amp;Timestamp=496707947.258279"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
2 changes: 1 addition & 1 deletion 13. Inheritance.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
1 change: 1 addition & 0 deletions 13. Inheritance.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ------------------------------------------------------------------------------------------------
// Checked September 2016
// Things to know:
//
// * There is no default base class for Swift objects. Any class that doesn't derive from
Expand Down
4 changes: 3 additions & 1 deletion 13. Inheritance.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4430&amp;EndingColumnNumber=5&amp;EndingLineNumber=7&amp;StartingColumnNumber=4&amp;StartingLineNumber=7&amp;Timestamp=429121270.152136">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4456&amp;EndingColumnNumber=5&amp;EndingLineNumber=8&amp;StartingColumnNumber=4&amp;StartingLineNumber=8&amp;Timestamp=496755665.442609"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
2 changes: 1 addition & 1 deletion 14a. Initialization.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
5 changes: 3 additions & 2 deletions 14a. Initialization.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ------------------------------------------------------------------------------------------------
// Checked and updated September 2016
// Things to know:
//
// * Swift provides an initializer (which partially resembles a function) to ensure that every
Expand Down Expand Up @@ -67,7 +68,7 @@ let freezingPointOfWater = Celsius(kelvin: 273.15)
// name generation and one that opts out:
struct Color
{
let red = 0.0, green = 0.0, blue = 0.0
var red = 0.0, green = 0.0, blue = 0.0

// This initializer will make use of automatically generated exernal names
init(red: Double, green: Double, blue: Double)
Expand Down Expand Up @@ -116,7 +117,7 @@ class SurveyQuestion
class SurveyQuestion2
{
// Default value of "No question"
let text: String = "No question"
var text: String = "No question"

var response: String?

Expand Down
4 changes: 3 additions & 1 deletion 14a. Initialization.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=7144&amp;EndingColumnNumber=5&amp;EndingLineNumber=9&amp;StartingColumnNumber=4&amp;StartingLineNumber=9&amp;Timestamp=424377229.524799">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=7182&amp;EndingColumnNumber=5&amp;EndingLineNumber=10&amp;StartingColumnNumber=4&amp;StartingLineNumber=10&amp;Timestamp=496755767.283091"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
2 changes: 1 addition & 1 deletion 14b. Initializer Chaining.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
5 changes: 3 additions & 2 deletions 14b. Initializer Chaining.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ------------------------------------------------------------------------------------------------
// Checked and updated September 2016
// Things to know:
//
// * Initializer Chaining refers to the way in which initialization takes place along the class
Expand Down Expand Up @@ -190,5 +191,5 @@ struct CheckerBoard

// We can now check our work
var board = CheckerBoard()
board.squareIsBlackAtRow(1, column: 1) // Should be false
board.squareIsBlackAtRow(1, column: 2) // Should be true
board.squareIsBlackAtRow(row: 1, column: 1) // Should be false
board.squareIsBlackAtRow(row: 1, column: 2) // Should be true
4 changes: 3 additions & 1 deletion 14b. Initializer Chaining.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=7452&amp;EndingColumnNumber=5&amp;EndingLineNumber=26&amp;StartingColumnNumber=4&amp;StartingLineNumber=26&amp;Timestamp=429121086.671912">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=7500&amp;EndingColumnNumber=5&amp;EndingLineNumber=27&amp;StartingColumnNumber=4&amp;StartingLineNumber=27&amp;Timestamp=496755871.982254"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
2 changes: 1 addition & 1 deletion 15. Deinitialization.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
14 changes: 8 additions & 6 deletions 15. Deinitialization.playground/section-1.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ------------------------------------------------------------------------------------------------
// Checked and updated September 2016
// Things to know:
//
// * Deinitializers are called automatically before a class instance is
Expand All @@ -17,9 +18,10 @@
struct Bank
{
static var coinsInBank = 10_000
static func vendCoins(var numberOfCoinsToVend: Int) -> Int
{
numberOfCoinsToVend = min(numberOfCoinsToVend, coinsInBank)
static func vendCoins(numberOfCoinsToVend: Int) -> Int
{
var numberOfCoinsToVend = numberOfCoinsToVend
numberOfCoinsToVend = min(numberOfCoinsToVend, coinsInBank)
coinsInBank -= numberOfCoinsToVend
return numberOfCoinsToVend
}
Expand All @@ -36,7 +38,7 @@ class Player

init(coins: Int)
{
coinsInPurse = Bank.vendCoins(coins)
coinsInPurse = Bank.vendCoins(numberOfCoinsToVend: coins)
}

func winCoins(coins: Int)
Expand All @@ -46,7 +48,7 @@ class Player

deinit
{
Bank.receiveCoins(coinsInPurse)
Bank.receiveCoins(coins: coinsInPurse)
}
}

Expand All @@ -57,7 +59,7 @@ playerOne!.coinsInPurse
Bank.coinsInBank

// The Player now wins 2000 coins!
playerOne!.winCoins(2_000)
playerOne!.winCoins(coins: 2_000)
playerOne!.coinsInPurse
Bank.coinsInBank

Expand Down
4 changes: 3 additions & 1 deletion 15. Deinitialization.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=1921&amp;EndingColumnNumber=5&amp;EndingLineNumber=1&amp;StartingColumnNumber=4&amp;StartingLineNumber=1&amp;Timestamp=424365502.348442">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2044&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=1&amp;Timestamp=496756034.689658"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
2 changes: 1 addition & 1 deletion 18. Type Casting.playground/contents.xcplayground
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Loading