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

upgraded to XCode 7.2 / Swift 2 #19

Open
wants to merge 53 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
36f9b8f
upgrade 1a, gitignore tmp/user files
ultrasaurus Dec 22, 2015
e455de7
println has been renamed to print
ultrasaurus Dec 22, 2015
8068575
added explanation of 0xC.3p0
ultrasaurus Dec 23, 2015
316bdb0
upgrade 1b
ultrasaurus Dec 23, 2015
3da0447
upgrade playground, add examples and note
ultrasaurus Dec 23, 2015
25872b9
upgrade to Xcode 7
ultrasaurus Dec 23, 2015
7eccba0
switch from .None to nil, .toInt -> Int()
ultrasaurus Dec 23, 2015
7c400a0
upgrade 1e - no changes needed
ultrasaurus Dec 23, 2015
e653857
upgrade 2.Basic Operations - no changes needed
ultrasaurus Dec 24, 2015
75bdce3
upgrade 3.Strings and Characters
ultrasaurus Dec 24, 2015
affd964
new String syntax
ultrasaurus Dec 24, 2015
57314dc
upgrade 4a.Arrays
ultrasaurus Dec 24, 2015
de35a19
enumerate is now a method on SequenceType
ultrasaurus Dec 24, 2015
9f0d993
upgrade 4b.Dictionaries - no changes needed
ultrasaurus Dec 24, 2015
6c3d358
upgrade 5. Control Flow
ultrasaurus Dec 24, 2015
0fecb13
fix syntax, clarify a couple of things
ultrasaurus Dec 24, 2015
6907e14
upgrade 6.Functions
ultrasaurus Dec 24, 2015
d575cb7
syntax fixes for Swift 2, minor text correction
ultrasaurus Dec 24, 2015
a0ca477
upgrade 7.Closures
ultrasaurus Dec 24, 2015
06c9e20
sorted -> sort
ultrasaurus Dec 24, 2015
ca5b7c6
upgrade 8.Enumerations
ultrasaurus Dec 24, 2015
486d14b
fix typos
ultrasaurus Dec 24, 2015
e3e7103
upgrade 9.Classes & Structures - no changes needed
ultrasaurus Dec 24, 2015
3f87b95
upgrade 10.Properties
ultrasaurus Dec 24, 2015
17963d6
alternate lazy property example
ultrasaurus Dec 24, 2015
96c8d79
upgrade 11.Methods
ultrasaurus Dec 24, 2015
7ec47ab
# has been removed from Swift
ultrasaurus Dec 24, 2015
fddb4d0
upgrade 12.Subscripts - no changes needed
ultrasaurus Dec 24, 2015
590d77a
upgrade 13.Inheritance
ultrasaurus Dec 25, 2015
79c400b
fix some comments that were missing words
ultrasaurus Dec 25, 2015
28b0c9d
upgrade 14.Initialization
ultrasaurus Dec 25, 2015
78eab50
fix syntax errors
ultrasaurus Dec 25, 2015
f3ec409
upgrade 14b.Initializer Chaining
ultrasaurus Dec 25, 2015
6525d2a
added annotations to illustrate init order
ultrasaurus Dec 26, 2015
14a78a1
upgrade 15.Deinitialization
ultrasaurus Dec 26, 2015
8d4914b
playground bug appears to have been fixed
ultrasaurus Dec 26, 2015
6ca17bc
upgrade 16.ARC
ultrasaurus Dec 26, 2015
df4c889
fix syntax error & typos
ultrasaurus Dec 26, 2015
856d43e
upgrade 17.Optional Chaining - no changes needed
ultrasaurus Dec 26, 2015
8df4447
upgrade 18.Type Casting & fix syntax errors
ultrasaurus Dec 26, 2015
255d824
upgrade 19.Nested Types
ultrasaurus Dec 26, 2015
13966b5
added an extra example
ultrasaurus Dec 26, 2015
9c7d728
upgrade 20.Extensions
ultrasaurus Dec 26, 2015
cfd8b7d
fixed syntax errors & warnings
ultrasaurus Dec 26, 2015
ba90335
upgrade 21.Protocols
ultrasaurus Dec 26, 2015
ad9b17c
fix @objc syntax + typos
ultrasaurus Dec 27, 2015
9450c5e
upgrade 22.Generics
ultrasaurus Dec 27, 2015
38a41da
fixed syntax for implicit arguments, enumerate
ultrasaurus Dec 27, 2015
117e927
upgrade 23.Advanced Operations
ultrasaurus Dec 27, 2015
4f179c3
removed &/ and &%
ultrasaurus Dec 27, 2015
2ceb7a8
upgrade 99.Not the End
ultrasaurus Dec 27, 2015
b3eae7b
fix syntax error (# shorthand removed from Swift2)
ultrasaurus Dec 27, 2015
0c382a2
update README for XCode version and overview
ultrasaurus Dec 27, 2015
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
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Mac hidden files
.DS_Store
.Trashes

# vim
*.swp

### Xcode ###
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.xcuserstate
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// * Properties store values in classes, structures and enumerations.
// ------------------------------------------------------------------------------------------------

// Here's a structure with a couple simple stored properties:
// Here's a structure with a couple of simple stored properties:
struct FixedLengthRange
{
var firstValue: Int
Expand Down Expand Up @@ -33,39 +33,48 @@ rangeOfThreeItems.firstValue = 6
// 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
// assume that DataImporter has a time-consuming process to fetch text, so we will want to use a lazy
// stored property whenever we use it. This way, if we end up never using it, we never pay the
// penalty of instantiating it.
// penalty of instantiating it and running the time-consuming import.
class DataImporter
{
var filename = "data.txt"
init() {
print("importing text")
}
}

class Something
{
init() {
print("doing some thing")
}
}

class DataManager
{
lazy var importer = DataImporter()
var data = [String]()
lazy var imported = DataImporter()
var data = [String]()
var thing = Something()
}

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

// Notice how we haven't used the importer yet, so it is nil:
manager
// Notice before we used the importer, "doing some thing" was printed as soon as we created the DataManager()
// illustrating that normal variables are instantiated running all associated code. Then "manager loaded" is
// printed before "importing text" which shows that we can use an instance of a class without causing the
// lazy variables to evaluate
manager.imported

// So now let's access it:
manager.importer.filename

// And now we see the importer was created:
manager

// ------------------------------------------------------------------------------------------------
// Computed Properties
//
// Computed properties don't store data, but rather use getters and setters for accessing values
// that are computed up on request.
// that are computed upon request.
//
// Computed Properties are available for global as well as local variables.
//
Expand All @@ -74,30 +83,32 @@ struct Point
{
var x = 0.0, y = 0.0
}

struct Size
{
var width = 0.0, height = 0.0
}

// The following structure includes a computed property with a Point type named 'center'. Notice
// that 'center' is variable (not constant) which is a requirement of computed properties.
//
// Every computed property must have a getter, but does not need a setter. If we provide a setter,
// we need to know the new value being assigned to the computed property. We've called this
// value 'newCenter'. Note that this value does not have a type annotation because the computed
// property already knows that it is a Point type. Providing a type annotation would be an error.
struct Rect
{
var origin = Point()
var size = Size()
var center: Point
{
// Every computed property must have a getter
get
{
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
// Setters are optional. In this case, we provide one.
// It needs to accept the new value being assigned to the computed property.
// We've called this value 'newCenter'.
// Note the missing type annotation. The computed property already knows that
// it is a Point type. Providing a type annotation would be an error.
set(newCenter)
{
origin.x = newCenter.x - (size.width / 2)
Expand All @@ -113,12 +124,17 @@ var square = Rect(origin: Point(x: 0.0, y: 0.0), size: Size(width: 10.0, height:
// property, we can treat it just like any other peroperty.
let initialSquareCenter = square.center

// We can see it has been comuputed as 5,5
square.center.x
square.center.y

// Since we provided a setter, we can also set the center point as if it is a stored property.
// This will effectively update the Rect's origin and size based on the specified center point.
square.center = Point(x: 15, y: 15)

// We can see that the origin has been updated from (0, 0) to (10, 10):
square.origin
square.origin.x
square.origin.y

// Shorthand Setter Declaration
//
Expand Down Expand Up @@ -299,3 +315,5 @@ class SomeClass
class var computedTypeProperty: Int { return 4 }
}



7 changes: 1 addition & 6 deletions 10. Properties.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='3.0' sdk='iphonesimulator'>
<sections>
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
<playground version='6.0' target-platform='ios' display-mode='raw'/>

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

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=10655&amp;EndingColumnNumber=5&amp;EndingLineNumber=2&amp;StartingColumnNumber=4&amp;StartingLineNumber=2&amp;Timestamp=472690804.766552"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class SomeClass
//
// For methods, the default behavior is that the caller must always specify all but the first
// external parameter name when calling the method. Member authors need not specify the external
// names for those parameters as the default is to treat all parameters as if they had the "#"
// specifier, which creates an external parameter name that mirrors the local parameter name.
// names for those parameters as the default with an external parameter name that mirrors
// the local parameter name.
//
// To override this default-external-names-for-second-and-beyond-parameters, specify an "_" as the
// external parameter name for all but the first parameter.
//
// If you want the caller to also use external name for the first parameter, be sure to add your
// own '#' symbol to the local name or specify the external name explicitly.
// If you want the caller to also use external name for the first parameter, be sure to 'double up'
// the local name specifying the external name explicitly.
//
// Here's a class that exercises the various combinations of internal and external name usages:
class Counter
Expand Down Expand Up @@ -77,10 +77,10 @@ class Counter
count += second
}

// 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
// Two parameters. Require caller to use external parameter name on first parameter
// and default to shared local/external names on the second.
// for the rest.
func addTwiceWithExternalSpecified2(#first: Int, second: Int)
func addTwiceWithExternalSpecified2(first first: Int, second: Int)
{
count += first
count += second
Expand Down Expand Up @@ -143,7 +143,7 @@ let fixedPoint = Point2(x: 3)
//
// fixedPoint.setX(4)

// If you're working with a structure or enumeration (not a class), uou can assign to 'self'
// If we're working with a structure or enumeration (not a class), we can assign to 'self'
// directly
struct Point3
{
Expand Down
7 changes: 1 addition & 6 deletions 11. Methods.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='3.0' sdk='iphonesimulator'>
<sections>
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
<playground version='6.0' target-platform='ios' display-mode='raw'/>

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

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=6194&amp;EndingColumnNumber=5&amp;EndingLineNumber=8&amp;StartingColumnNumber=4&amp;StartingLineNumber=8&amp;Timestamp=472691471.401266"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
7 changes: 1 addition & 6 deletions 12. Subscripts.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='3.0' sdk='iphonesimulator'>
<sections>
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
<playground version='6.0' target-platform='ios' display-mode='raw'/>

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

4 changes: 3 additions & 1 deletion 12. Subscripts.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=2316&amp;EndingColumnNumber=5&amp;EndingLineNumber=15&amp;StartingColumnNumber=4&amp;StartingLineNumber=15&amp;Timestamp=424368933.017819">
documentLocation = "#CharacterRangeLen=0&amp;CharacterRangeLoc=2316&amp;EndingColumnNumber=5&amp;EndingLineNumber=15&amp;StartingColumnNumber=4&amp;StartingLineNumber=15&amp;Timestamp=424368933.017819"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ automaticCar.speed = 35.0
automaticCar.gear

// ------------------------------------------------------------------------------------------------
// Preenting Overrides
// Preventing Overrides
//
// 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
//
// Here, we'll prevent an entire class from being subclassed by applying the . Because of this,
// Here, we'll prevent an entire class from being subclassed by applying final to the class. Therefore,
// 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
Expand Down
7 changes: 1 addition & 6 deletions 13. Inheritance.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='3.0' sdk='iphonesimulator'>
<sections>
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
<playground version='6.0' target-platform='ios' display-mode='raw'/>
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=4439&amp;EndingColumnNumber=5&amp;EndingLineNumber=7&amp;StartingColumnNumber=4&amp;StartingLineNumber=7&amp;Timestamp=472755223.513943"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,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 +116,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 Expand Up @@ -148,14 +148,7 @@ let beetsQuestion = SurveyQuestion2(text: "Do you like beets?")
// ------------------------------------------------------------------------------------------------
// Default Initializer
//
// If all properties have default values (including optionals defaulting to nil) AND you do not
// create your own initlializer AND there is no superclass, Swift will create a default
// initializer (with no parameters) for you. This initializer sets all properties to their
// default values.
//
// If you create your own initializer, Swift will not create the default initializer. If you want
// your custom initializers as well as the default initializer, then put your initializers in an
// extension.
// Swift sets all properties to their default values even if you don't have an initializer.
class ShoppingListItem
{
var name: String?
Expand All @@ -165,6 +158,25 @@ class ShoppingListItem
// No init(...) initializer
}

ShoppingListItem().quantity

// If you have an initializer, default values are set before your initializer executes.

class ShoppingListItem2
{
var name: String?
var quantity = 1
var purchased = false

init()
{
print("ShoppingListItem2: by default quantity = \(quantity)")
}
}

ShoppingListItem2()


// ------------------------------------------------------------------------------------------------
// Memberwise Initializers for Structure Types
//
Expand Down
7 changes: 1 addition & 6 deletions 14a. Initialization.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='3.0' sdk='iphonesimulator'>
<sections>
<code source-file-name='section-1.swift'/>
</sections>
<timeline fileName='timeline.xctimeline'/>
</playground>
<playground version='6.0' target-platform='ios' display-mode='raw'/>
Loading