Skip to content

Commit

Permalink
Updates to bring up to date with XCode6 Beta5
Browse files Browse the repository at this point in the history
  • Loading branch information
nettlep committed Aug 7, 2014
1 parent 8592722 commit af42c38
Show file tree
Hide file tree
Showing 27 changed files with 81 additions and 147 deletions.
11 changes: 6 additions & 5 deletions 10. Properties.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ rangeOfThreeItems.firstValue = 6
//
// A Lazy Stored Property is a value that is not calculated until its first use.
//
// They are declared using the "@lazy" attribute and may not be constant.
// They are declared using the "lazy" attribute and may not be constant.
//
// Global and local variables are all lazy, except that they don't need the @lazy attribute.
// Global and local variables are all lazy, except that they don't need the lazy attribute.
//
// Here, we'll define a couple classes to showcase Lazy Stored Properties. In this example, let's
// assume that DataImporter is a time-consuming process, and as such, we will want to use a lazy
Expand All @@ -43,14 +43,14 @@ class DataImporter

class DataManager
{
@lazy var importer = DataImporter()
lazy var importer = DataImporter()
var data = [String]()
}

// Now let's instantiate the data manager and add some simple data to the class:
let manager = DataManager()
manager.data += "Some data"
manager.data += "Some more data"
manager.data.append("Some data")
manager.data.append("Some more data")

// Notice how we haven't used the importer yet, so it is nil:
manager
Expand Down Expand Up @@ -298,3 +298,4 @@ class SomeClass
// This is read-only, but you can also do read/write
class var computedTypeProperty: Int { return 4 }
}

2 changes: 1 addition & 1 deletion 10. Properties.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=10168&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=424364807.022397">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=10176&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=429121248.87547">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
20 changes: 10 additions & 10 deletions 13. Inheritance.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Vehicle
class Bicycle: Vehicle
{
// We'll make this a 2-wheeled vehicle
init()
override init()
{
super.init()
numberOfWheels = 2
Expand All @@ -46,7 +46,7 @@ bicycle.description()
class Tandem: Bicycle
{
// This bicycle has 2 passengers
init()
override init()
{
super.init()
maxPassengers = 2
Expand All @@ -61,7 +61,7 @@ class Car: Vehicle
var speed: Double = 0.0

// New initialization, starting with super.init()
init()
override init()
{
super.init()
maxPassengers = 5
Expand Down Expand Up @@ -143,21 +143,21 @@ automaticCar.gear
// ------------------------------------------------------------------------------------------------
// Preenting Overrides
//
// We can prevent a subclass from overriding a particular method or property using the '@final'
// We can prevent a subclass from overriding a particular method or property using the 'final'
// keyword.
//
// @final can be applied to: class, var, func, class methods and subscripts
// final can be applied to: class, var, func, class methods and subscripts
//
// Here, we'll prevent an entire class from being subclassed by applying the . Because of this,
// the @finals inside the class are not needed, but are present for descriptive purposes. These
// additional @finals may not compile in the future, but they do today:
@final class AnotherAutomaticCar: Car
// the finals inside the class are not needed, but are present for descriptive purposes. These
// additional finals may not compile in the future, but they do today:
final class AnotherAutomaticCar: Car
{
// This variable cannot be overridden
@final var gear = 1
final var gear = 1

// We can even prevent overridden functions from being further refined
@final override var speed: Double
final override var speed: Double
{
didSet { gear = Int(speed / 10.0) + 1 }
}
Expand Down
2 changes: 1 addition & 1 deletion 13. Inheritance.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4410&amp;EndingColumnNumber=5&amp;EndingLineNumber=7&amp;StartingColumnNumber=4&amp;StartingLineNumber=7&amp;Timestamp=424369866.603036">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4430&amp;EndingColumnNumber=5&amp;EndingLineNumber=7&amp;StartingColumnNumber=4&amp;StartingLineNumber=7&amp;Timestamp=429121270.152136">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
4 changes: 2 additions & 2 deletions 14b. Initializer Chaining.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class RecipeIngredient: Food
// Here, we'll create a convenience initializer that simply provides a default quantity
// value of 1. Note that in order for this to compile, we are required to call a designated
// initializer.
convenience init(name: String)
convenience override init(name: String)
{
self.init(name: name, quantity: 1)
}
Expand All @@ -118,7 +118,7 @@ class ShoppingListItem: RecipeIngredient
var purchased = false
var description: String
{
var output = "\(quantity) x \(name.lowercaseString)"
var output = "\(quantity) x \(name)"
output += purchased ? "" : ""
return output
}
Expand Down
2 changes: 1 addition & 1 deletion 14b. Initializer Chaining.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=7459&amp;EndingColumnNumber=5&amp;EndingLineNumber=26&amp;StartingColumnNumber=4&amp;StartingLineNumber=26&amp;Timestamp=424379045.424535">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=7452&amp;EndingColumnNumber=5&amp;EndingLineNumber=26&amp;StartingColumnNumber=4&amp;StartingLineNumber=26&amp;Timestamp=429121086.671912">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
14 changes: 7 additions & 7 deletions 16. ARC.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,16 @@ var america = Country(name: "USA", capitalName: "Washington DC")
// Let's see how this problem can manifest. We'll create a class that represents an HTML element
// which includes a variable (asHTML) which stores a reference to a closure.
//
// Quick note: The asHTML variable is defined as @lazy so that it can reference 'self' within the
// closure. Try removing the '@lazy' and you'll see that you get an error trying to access 'self'.
// Quick note: The asHTML variable is defined as lazy so that it can reference 'self' within the
// closure. Try removing the 'lazy' and you'll see that you get an error trying to access 'self'.
// This is an error because we're not allowed to access 'self' during Phase 1 initialization. By
// making 'asHTML' @lazy, we solve this problem by deferring its initialization until later.
// making 'asHTML' lazy, we solve this problem by deferring its initialization until later.
class HTMLElement
{
let name: String
let text: String?

@lazy var asHTML: () -> String =
lazy var asHTML: () -> String =
{
if let text = self.text
{
Expand Down Expand Up @@ -321,7 +321,7 @@ paragraph = nil
//
// Here's how we define a capture list:
//
// @lazy var someClosure: (Int, String) -> String =
// lazy var someClosure: (Int, String) -> String =
// {
// [unowned self] (index: Int, stringToProcess: String) -> String in
//
Expand All @@ -332,7 +332,7 @@ paragraph = nil
// may not have any parameters. In both cases the method for declaring the capture list doesn't
// change much. Simply include the capture list followed by the 'in' keyword:
//
// @lazy var someClosure: () -> String =
// lazy var someClosure: () -> String =
// {
// [unowned self] in
//
Expand All @@ -347,7 +347,7 @@ class FixedHTMLElement
let name: String
let text: String?

@lazy var asHTML: () -> String =
lazy var asHTML: () -> String =
{
[unowned self] in
if let text = self.text
Expand Down
2 changes: 1 addition & 1 deletion 16. ARC.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=14330&amp;EndingColumnNumber=5&amp;EndingLineNumber=17&amp;StartingColumnNumber=4&amp;StartingLineNumber=17&amp;Timestamp=424544773.38719">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=14323&amp;EndingColumnNumber=5&amp;EndingLineNumber=17&amp;StartingColumnNumber=4&amp;StartingLineNumber=17&amp;Timestamp=429121230.809536">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
7 changes: 3 additions & 4 deletions 1a. The Basics.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ let π = 3.14159
let 你好 = "你好世界"
let 🐶🐮 = "dogcow"

// You can print a value using println (this doesn't do anything in a playground, though)
// You can print a value using println
let fiveHundred = 500
println("The current value of fiveHundred is: \(fiveHundred)")

// Since println doesn't work in Playgrounds, we'll just put the raw string on the line
// which is an expression that evaluates to itself, printing the result in the right-hand
// pane in the playground, like so:
// Since we're using Playgrounds, we'll just put the raw string on the line which is an expression
// that evaluates to itself, printing the result in the right-hand pane in the playground, like so:
"The current value of fiveHundred is: \(fiveHundred)"

// ------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions 1a. The Basics.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=8885&amp;EndingColumnNumber=5&amp;EndingLineNumber=4&amp;StartingColumnNumber=4&amp;StartingLineNumber=4&amp;Timestamp=424673361.066013">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=8819&amp;EndingColumnNumber=5&amp;EndingLineNumber=4&amp;StartingColumnNumber=4&amp;StartingLineNumber=4&amp;Timestamp=429119882.388055">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=17&amp;CharacterRangeLoc=8062&amp;EndingColumnNumber=21&amp;EndingLineNumber=224&amp;StartingColumnNumber=4&amp;StartingLineNumber=224&amp;Timestamp=424673361.066013">
documentLocation = "#CharacterRangeLen=17&amp;CharacterRangeLoc=7996&amp;EndingColumnNumber=21&amp;EndingLineNumber=223&amp;StartingColumnNumber=4&amp;StartingLineNumber=223&amp;Timestamp=429119882.388055">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
15 changes: 8 additions & 7 deletions 1d. Optionals.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
// ------------------------------------------------------------------------------------------------

// An optional declaration adds a "?" immediately after the explicit type. The following line
// defines a value 'someOptional' that can either hold an Int or no value at all:
let someOptional: Int?
// defines a value 'someOptional' that can either hold an Int or no value at all. In this case
// we set an optional Int value to .None (similar to nil)
let someOptional: Int? = .None

// Let's try to convert a String to an Int
//
Expand Down Expand Up @@ -43,8 +44,8 @@ let unwrapped = optionalConvertedNumber // 'unwrapped' is another optional
// let's not let that stop us from learning this little detail.
//
// These two lines are of equivalent types:
let optionalA: String?
let optionalB: Optional<String>
let optionalA: String? = .None
let optionalB: Optional<String> = .None

// ------------------------------------------------------------------------------------------------
// Unwrapping
Expand All @@ -64,7 +65,7 @@ let unwrappedInt = optionalConvertedNumber!
// Implicit unwrapping isn't very safe because if the optional doesn't hold a value, it will
// generate a runtime error. To verify that is's safe, you can check the optional with an if
// statement.
if optionalConvertedNumber
if optionalConvertedNumber != .None
{
// It's now safe to force-unwrap because we KNOW it has a value
let anotherUnwrappedInt = optionalConvertedNumber!
Expand Down Expand Up @@ -103,7 +104,7 @@ if let optionalIntValue:Int? = optionalConvertedNumber
// 'optionalIntValue' is still an optional, but it's known to be safe. We can still check
// it here, though, because it's still an optional. If it weren't optional, this if statement
// wouldn't compile:
if optionalIntValue
if optionalIntValue != .None
{
// 'optionalIntValue' is optional, so we still use the force-unwrap here:
"intValue is optional, but has the value \(optionalIntValue!)"
Expand All @@ -114,7 +115,7 @@ if let optionalIntValue:Int? = optionalConvertedNumber
optionalConvertedNumber = nil

// Now if we check it, we see that it holds no value:
if optionalConvertedNumber
if optionalConvertedNumber != .None
{
"optionalConvertedNumber holds a value (\(optionalConvertedNumber))! (this should not happen)"
}
Expand Down
2 changes: 1 addition & 1 deletion 1d. Optionals.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=6821&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=428937322.498252">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=6943&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=429120028.697866">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
2 changes: 1 addition & 1 deletion 1e. Assertions.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// ------------------------------------------------------------------------------------------------

// Let's start with a value...
let age = 3;
let age = 3

// You can assert with a message
assert(age >= 0, "A person's age cannot be negative")
Expand Down
2 changes: 1 addition & 1 deletion 1e. Assertions.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=577&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=424294298.504034">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=576&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=429120071.699096">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
4 changes: 2 additions & 2 deletions 20. Extensions.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ extension Int
subscript(digitIndex: Int) -> Int
{
var decimalBase = 1
for _ in 1...digitIndex
for _ in 0 ..< digitIndex
{
decimalBase *= 10
}
Expand Down Expand Up @@ -170,7 +170,7 @@ extension Character
}
var kind: Kind
{
switch String(self).lowercaseString
switch String(self)
{
case "a", "e", "i", "o", "u":
return .Vowel
Expand Down
2 changes: 1 addition & 1 deletion 20. Extensions.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=5391&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=424630267.973389">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=5377&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=429121495.027786">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
29 changes: 6 additions & 23 deletions 21. Protocols.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Starship: FullyNamed

var fullName: String
{
return (prefix ? prefix! + " " : "") + name
return (prefix != .None ? prefix! + " " : "") + name
}
}

Expand Down Expand Up @@ -357,7 +357,7 @@ objects[2] is HasArea
// Optional Protocol Requirements
//
// Sometimes it's convenient to declare protocols that have one or more requirements that are
// optional. This is done by prefixing those requirements with the '@optional' keyword.
// optional. This is done by prefixing those requirements with the 'optional' keyword.
//
// The term "optional protocol" refers to protocols that are optional in a very similar since to
// optionals we've seen in the past. However, rather than stored values that can be nil, they
Expand All @@ -372,17 +372,17 @@ objects[2] is HasArea
// * In order to check if an instance of a class conforms to a given protocol, that protocol must
// be declared with the @objc attribute.
//
// * This is also the case with optional requirements in protocols. In order to use the @optional
// attribute, the protocol must be declared with the @objc attribute.
// * This is also the case with optional requirements in protocols. In order to use the optional
// declaration modifier, the protocol must be declared with the @objc attribute.
//
// * Additionally, any class, structure or enumeration that owns an instance that conforms to a
// protocol declared with the @objc attribute must also be declared with the @objc attribute.
//
// Here's another simple protocol that uses optional requrements:
@objc protocol CounterDataSource
{
@optional func incrementForCount(count: Int) -> Int
@optional var fixedIncrement: Int { get }
optional func incrementForCount(count: Int) -> Int
optional var fixedIncrement: Int { get }
}

// In the class below, we'll see that checking to see if an instance conforms to a specific
Expand All @@ -406,20 +406,3 @@ objects[2] is HasArea
}
}
}

















2 changes: 1 addition & 1 deletion 21. Protocols.playground/timeline.xctimeline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=13079&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=424650306.321352">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=13078&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=429121693.645013">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
Loading

0 comments on commit af42c38

Please sign in to comment.