Skip to content

Commit

Permalink
Merge branch 'ChilliCoders-master'
Browse files Browse the repository at this point in the history
Updated playgrounds to conform to latest Swift langauge changes that came with Xcode Beta 3.
  • Loading branch information
nettlep committed Jul 9, 2014
2 parents f2f0b2d + dd5a3cb commit 6244068
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 117 deletions.
4 changes: 2 additions & 2 deletions 10. Properties.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DataImporter
class DataManager
{
@lazy var importer = DataImporter()
var data = String[]()
var data = [String]()
}

// Now let's instantiate the data manager and add some simple data to the class:
Expand Down Expand Up @@ -297,4 +297,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 12. Subscripts.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct Matrix
{
let rows: Int
let columns: Int
var grid: Double[]
var grid: [Double]

init (rows: Int, columns: Int)
{
Expand Down
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 @@ -163,9 +163,9 @@ class ClassWithPI
// initialized and then returned.
struct CheckerBoard
{
let boardColors: Bool[] =
let boardColors: [Bool] =
{
var temporaryBoard = Bool[]()
var temporaryBoard = [Bool]()
var isBlack = false
for i in 1...10
{
Expand Down
8 changes: 4 additions & 4 deletions 18. Type Casting.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ for item in library
//
// Let's see AnyObject in action. We'll define an array of type AnyObject[] and populate it with
// some movies:
let someObjects: AnyObject[] =
let someObjects: [AnyObject] =
[
Movie(name: "2001: A Space Odyssey", director: "Stanley Kubrick"),
Movie(name: "Moon", director: "Duncan Jones"),
Expand All @@ -127,15 +127,15 @@ for object: AnyObject in someObjects
}

// Alternatively, we can downcast the array itself rather than each item:
var someMovies = someObjects as Movie[]
var someMovies = someObjects as [Movie]
for movie in someMovies
{
"Movie: '\(movie.name)' was directed by \(movie.director)"
}

// Finally, we can avoid the additional local variable and performt he downcast right inside
// the loop structure:
for movie in someObjects as Movie[]
for movie in someObjects as [Movie]
{
"Movie: '\(movie.name)' was directed by \(movie.director)"
}
Expand All @@ -145,7 +145,7 @@ for movie in someObjects as Movie[]
//
// Let's see this in action. We'll create an array of type Any[] and fill it with random bits and
// pieces of stuff:
var things = Any[]()
var things = [Any]()

things.append(0)
things.append(0.0)
Expand Down
2 changes: 1 addition & 1 deletion 2. Basic operations.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var d = a % b // Floating point remainder
// The range operator with two dots means up to but NOT including the final value.
//
// This is called the "Half-Closed Range Operator"
for i in 1..10
for i in 1..<10
{
i // prints 1 through 9
}
Expand Down
2 changes: 1 addition & 1 deletion 20. Extensions.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ extension Int
{
func repititions(task: () -> ())
{
for i in 0..self
for i in 0..<self
{
task()
}
Expand Down
4 changes: 2 additions & 2 deletions 21. Protocols.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ extension Hamster: TextRepresentable
// Hamsters and Dice don't have much in common, but in our sample code above, they both conform
// to the TextRepresentable protocol. Because of this, we can create an array of things that are
// TextRepresentable which includes each:
let textRepresentableThigns: TextRepresentable[] = [d6, tedTheHamster]
let textRepresentableThigns: [TextRepresentable] = [d6, tedTheHamster]

// We can now loop through each and produce its text representation:
for thing in textRepresentableThigns
Expand Down Expand Up @@ -341,7 +341,7 @@ class Animal
}

// We can store our objects into an array of type AnyObject[]
let objects: AnyObject[] =
let objects: [AnyObject] =
[
Circle(radius: 3.0),
Country(area: 4356947.0),
Expand Down
8 changes: 4 additions & 4 deletions 22. Generics.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ bString
// type for a property, the input parameter for a method and the return value for a method.
struct Stack<T>
{
var items = T[]()
var items = [T]()
mutating func push(item: T)
{
items.append(item)
Expand Down Expand Up @@ -133,7 +133,7 @@ func doSomethingWithKeyValue<KeyType: Hashable, ValueType>(someKey: KeyType, som
// element from the array with the value being searched for. By including the Equatable, we tell
// the generic function that it is guaranteed to receive only values that meet that specific
// criteria.
func findIndex<T: Equatable>(array: T[], valueToFind: T) -> Int?
func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int?
{
for (index, value) in enumerate(array)
{
Expand Down Expand Up @@ -177,7 +177,7 @@ struct StackContainer<T> : Container
{
// Here we find our original stack implementation, unmodified

var items = T[]()
var items = [T]()
mutating func push(item: T)
{
items.append(item)
Expand Down Expand Up @@ -251,7 +251,7 @@ func allItemsMatch
}

// Check each pair of items to see if they are equivalent
for i in 0..someContainer.count
for i in 0..<someContainer.count
{
if someContainer[i] != anotherContainer[i]
{
Expand Down
95 changes: 36 additions & 59 deletions 4a. Arrays.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
// * Arrays are type-safe and always clear about what they contain.
//
// * Arrays are value types, but Swift is smart about only copying when necessary to improve
// performance. This has important implications for immutability.
// performance.
//
// * Immutable arrays (constant arrays) can allow their contents to change, which differs from the
// other type of Collection, Dictionaries.
// * Immutable arrays are immutable in terms of the array itself and the contents of the array.
// This means you can't add/remove an element nor can you modify an element of an immutable
// array.
// ------------------------------------------------------------------------------------------------

// Create an array of Strings
var someArray = Array<String>()

// Shorter, more common way to define an array of Strings
var shorter: String[]
var shorter: [String]

// This is an array literal. Since all members are of type String, this will create a String array.
//
Expand All @@ -28,7 +29,7 @@ var shorter: String[]
["Eggs", "Milk"]

// Let's create an array with some stuff in it. We'll use an explicit String type:
var commonPets: String[] = ["Cats", "Dogs"]
var commonPets: [String] = ["Cats", "Dogs"]

// We can also let Swift infer the type of the Array based on the type of the initializer members.
//
Expand Down Expand Up @@ -71,7 +72,7 @@ shoppingList[0] = "Six Eggs"
shoppingList[4...6] = ["Banannas", "Apples"]

// Or we can replace two items with three, inserting a new item:
shoppingList[4..6] = ["Limes", "Mint leaves", "Sugar"]
shoppingList[4..<6] = ["Limes", "Mint leaves", "Sugar"]

// We can insert an item at a given index
shoppingList.insert("Maple Syrup", atIndex: 3)
Expand Down Expand Up @@ -102,7 +103,7 @@ for (index, value) in enumerate(shoppingList)
//
// Earlier, we saw how to declare an array of a given type. Here, we see how to declare an array
// type and then assign it to a stored value, which gets its type by inference:
var someInts = Int[]()
var someInts = [Int]()

// Add the number '3' to the array
someInts.append(3)
Expand All @@ -113,7 +114,7 @@ someInts
someInts = []

// We can initialize an array and and fill it with default values
var threeDoubles = Double[](count: 3, repeatedValue: 3.3)
var threeDoubles = [Double](count: 3, repeatedValue: 3.3)

// We can also use the Array initializer to fill it with default values. Note that we don't need to
// specify type since it is inferred:
Expand All @@ -126,10 +127,11 @@ let immutableArray = ["a", "b"]
// separately. Therefore, you can change the contents of an immutable array, but you can't change
// the array itself.
//
// We change the contents of an immutable array:
immutableArray[0] = "b"

// But if you try to change the size or add an element, you will get a compiler error:
// We can't change the contents of an immutable array:
//
// immutableArray[0] = "b"
//
// Nor can we change the size or add an element, you will get a compiler error:
//
// immutableArray += "c"

Expand All @@ -139,59 +141,43 @@ immutableArray[0] = "b"
// Arrays are value types that only copy when necessary, which is only when the array itself
// changes (not the contents.)
//
// Here are three copies of the same array:
// Here are three copies of an array:
var a = [1, 2, 3]
var b = a
var c = a

// They are all the same...
a[0]
b[0]
c[0]
// Swift uses an efficient lazy copy to manage memory for arrays. So for the time being, a, b & c
// all reference the same memory. (Note the use of '===' for testing if the objects are the same
// instance.)
if a === b { "a and b share the same elements" }
if b === c { "b and c share the same elements" }
if c === a { "c and a share the same elements" }

// Change one value within the array and they all change:
// However, if we change the contents of one array (mutating it), then it is copied and becomes its
// own unique entity:
a[0] = 42
b[0]
c[0]

// But if we mofify the array's size, then the array being mutated is copied and becomes its own
// unique entity.
a.append(4)
a[0] = 1

// Now, a is different from b and c
a
b
c
// Now that we've changed a, it should have been copied to its own instance. Let's double-check
// that only b & c are the same:
if a === b { "a and b share the same elements" }
if b === c { "b and c share the same elements" }
if c === a { "c and a share the same elements" }

// Since 'b' and 'c' effectivly share the same contents, you can force one to become unique without
// modifying the array's size using the Array's unshare() method.
//
// The unshare() method is performant because it doesn't actually copy the array contents until
// it has to (if ever.)
b.unshare()
// The same is true if we mutate the array in other ways (mofify the array's size)...
b.append(4)

// They still appear to be the same...
b
c
// And now they should all be unique...
if a === b { "a and b share the same elements" }
if b === c { "b and c share the same elements" }
if c === a { "c and a share the same elements" }

// ...but b is actually a unique copy. Let's change an element in b:
b[0] = 99

// And we can see that our change only affects b:
// Now, we have three different arrays...
a
b
c

// We can further verify this by comparing if they are the same instance:
if b === c
{
"b & c still share same array elements"
}
else
{
"b & c now refer to two independent arrays"
}

// This works with sub-arrays, too:
if b[0...1] === b[0...1]
{
Expand All @@ -201,12 +187,3 @@ else
{
"these guys are NOT shared"
}

// Forcing a copy of an array
//
// Use the copy() method to force a shallow copy.
//
// Unlike the unshare method, the copy will happen immediately when calling copy().
var d = a.copy()
a[0] = 101
d[0]
2 changes: 1 addition & 1 deletion 4a. Arrays.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=6385&amp;EndingColumnNumber=5&amp;EndingLineNumber=16&amp;StartingColumnNumber=4&amp;StartingLineNumber=16&amp;Timestamp=424365425.423988">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=6335&amp;EndingColumnNumber=5&amp;EndingLineNumber=17&amp;StartingColumnNumber=4&amp;StartingLineNumber=17&amp;Timestamp=426611031.566827">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
11 changes: 8 additions & 3 deletions 4b. Dictionaries.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
// Let's use that literal to define and initialize a Dictionary.
//
// In this case, we use type annotation to explicitly declare a Dictionary containing String keys
// and String values:
var airports: Dictionary<String, String> = ["TYO": "Tokyo", "DUB": "Dublin", "APL": "Apple Intl"]
// and String values. This uses the syntactic sugar "[ KeyType: ValueType ]" to declare the
// dictionary.
var airports: [String : String] = ["TYO": "Tokyo", "DUB": "Dublin", "APL": "Apple Intl"]

// The declaration for airports above could also have been declared in this way:
var players: Dictionary<String, String> = ["Who" : "First", "What" : "Second"]

// In the case below, the literal contains only Strings for all keys and only Strings for all
// values, so type inference works in our favor allowing us to avoid the type annotation:
Expand Down Expand Up @@ -102,7 +106,7 @@ namesOfIntegers = [:]
// An immutable dictionary is a constant.
let immutableDict = ["a": "one", "b": "two"]

// Unlike arrays, you cannot modify the contents of an immutable dictionary. The following lines
// Similar to arrays, we cannot modify the contents of an immutable dictionary. The following lines
// will not compile:
//
// immutableDict["a"] = "b" // You cannot modify an element
Expand All @@ -119,3 +123,4 @@ copiedAges["Peter"] = 24

// And we can see that the original is not changed:
ages["Peter"]

4 changes: 2 additions & 2 deletions 4b. Dictionaries.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=4582&amp;EndingColumnNumber=5&amp;EndingLineNumber=16&amp;StartingColumnNumber=4&amp;StartingLineNumber=16&amp;Timestamp=424365435.370573">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=4822&amp;EndingColumnNumber=5&amp;EndingLineNumber=16&amp;StartingColumnNumber=4&amp;StartingLineNumber=16&amp;Timestamp=426611538.879704">
</LoggerValueHistoryTimelineItem>
<LoggerValueHistoryTimelineItem
documentLocation = "#CharacterRangeLen=11&amp;CharacterRangeLoc=3146&amp;EndingColumnNumber=12&amp;EndingLineNumber=73&amp;StartingColumnNumber=1&amp;StartingLineNumber=73&amp;Timestamp=424365435.370573">
documentLocation = "#CharacterRangeLen=11&amp;CharacterRangeLoc=3383&amp;EndingColumnNumber=12&amp;EndingLineNumber=77&amp;StartingColumnNumber=1&amp;StartingLineNumber=77&amp;Timestamp=426611538.879704">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
6 changes: 3 additions & 3 deletions 5. Control Flow.playground/section-1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// ------------------------------------------------------------------------------------------------
// For loops
//
// We can loop through ranges using the closed-range operator (three dots).
// We can loop through ranges using the closed-range operator ("...").
//
// In the loop below, 'index' is a constant that is automatically declared.
for index in 1...5
Expand All @@ -26,10 +26,10 @@ for index in 1...5
//
// index = 0

// We can loop through ranges using the half-closed range operator (with two dots)
// We can loop through ranges using the half-closed range operator ("..<")
//
// We can also reuse the name 'index' because of the scoping noted previously.
for index in 1..5
for index in 1 ..< 5
{
"This will print 4 times"
}
Expand Down
2 changes: 1 addition & 1 deletion 5. Control Flow.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=11004&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=424365450.220564">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=10993&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=426611652.098414">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
Loading

0 comments on commit 6244068

Please sign in to comment.