Skip to content

Commit af4c7da

Browse files
committed
Anticipate Swift 2.2 / 3: var parameters (including if var), C for loops, implicit curry, inc/dec operators, early failable init exit
1 parent 0be3642 commit af4c7da

File tree

21 files changed

+105
-45
lines changed

21 files changed

+105
-45
lines changed

bk1ch02p038modifiableParameters/bk1ch02p038modifiableParameters/Base.lproj/Main.storyboard

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10102" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10080"/>
55
</dependencies>
66
<scenes>
77
<!--View Controller-->
88
<scene sceneID="ufC-wZ-h7g">
99
<objects>
10-
<viewController id="vXZ-lx-hvc" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
10+
<viewController id="vXZ-lx-hvc" customClass="ViewController" customModule="bk1ch02p038modifiableParameters" customModuleProvider="target" sceneMemberID="viewController">
1111
<layoutGuides>
1212
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
1313
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>

bk1ch02p038modifiableParameters/bk1ch02p038modifiableParameters/ViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import UIKit
44

5+
// WARNING: This may go away, because it is, after all, rather misleading
6+
57
func say(s:String, times:Int, var loudly:Bool) {
68
loudly = true // can't do this without "var"
79
}
810

11+
// WARNING: This may go away, because it is, after all, rather misleading
12+
913
func removeFromStringNot(var s:String, character c:Character) -> Int {
1014
var howMany = 0
1115
while let ix = s.characters.indexOf(c) {

bk1ch02p052closures/bk1ch02p052closures/ViewController.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ func makeRoundedRectangleMaker3(sz:CGSize) -> (CGFloat) -> UIImage {
109109
}
110110
}
111111

112-
// implicit curry
112+
113+
// implicit curry: deprecated in Swift 2.2, slated for removal in Swift 3
114+
115+
/*
116+
113117
func makeRoundedRectangleMaker4(sz:CGSize)(_ r:CGFloat) -> UIImage {
114118
return imageOfSize(sz) {
115119
let p = UIBezierPath(
@@ -119,6 +123,7 @@ func makeRoundedRectangleMaker4(sz:CGSize)(_ r:CGFloat) -> UIImage {
119123
}
120124
}
121125

126+
*/
122127

123128

124129

@@ -213,11 +218,11 @@ class ViewController: UIViewController {
213218
let image1 = makeRoundedRectangleMaker3(CGSizeMake(45,20))(8)
214219
_ = image1
215220

216-
let maker4 = makeRoundedRectangleMaker4(CGSizeMake(45,20))
217-
self.myImageView.image = maker4(8)
218-
219-
let image2 = makeRoundedRectangleMaker4(CGSizeMake(45,20))(8)
220-
_ = image2
221+
// let maker4 = makeRoundedRectangleMaker4(CGSizeMake(45,20))
222+
// self.myImageView.image = maker4(8)
223+
//
224+
// let image2 = makeRoundedRectangleMaker4(CGSizeMake(45,20))(8)
225+
// _ = image2
221226

222227

223228
}

bk1ch03p092characterAndRange/bk1ch03p092stringAndRange/ViewController.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@ class ViewController: UIViewController {
101101
print(c)
102102
}
103103

104-
do {
105-
let s = "hello"
106-
var ix = s.startIndex
107-
let c = s[++ix] // "e"
108-
print(c)
109-
}
104+
// example removed: ++ operator deprecated in Swift 2.2, will be removed in Swift 3
105+
// do {
106+
// let s = "hello"
107+
// var ix = s.startIndex
108+
// let c = s[++ix] // "e"
109+
// print(c)
110+
// }
110111

111112

112113
do {
@@ -169,11 +170,12 @@ class ViewController: UIViewController {
169170
print(s2)
170171
}
171172

173+
// removed ++ and -- from this example
172174
do {
173175
let s = "hello"
174176
var r = s.characters.indices
175-
r.startIndex++
176-
r.endIndex--
177+
r.startIndex = r.startIndex.successor()
178+
r.endIndex = r.endIndex.predecessor()
177179
let s2 = s[r] // "ell"
178180
print(s2)
179181
}

bk1ch04p115initializers/bk1ch04p115initializers/ViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ class DogFailable {
125125
let name : String
126126
let license : Int
127127
init!(name:String, license:Int) {
128-
self.name = name
129-
self.license = license
130128
if name.isEmpty {
131-
return nil
129+
return nil // early exit is legal for a class in Swift 2.2
132130
}
133131
if license <= 0 {
134132
return nil
135133
}
134+
self.name = name
135+
self.license = license
136136
}
137137
}
138138

bk1ch04p153classInitializers/bk1ch04p153classInitializers/ViewController.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ class NoisyDog9 : Dog9 {
108108
return nil // legal
109109
}
110110
init?(ok:Bool) {
111-
// return nil // compile error: "all stored properties... must be initialized..."
111+
if !ok {return nil}
112+
// used to give compile error: "all stored properties... must be initialized..."
113+
// now legal starting in Swift 2.2
112114
super.init(name:"Fido", license:123)
113-
return nil // _now_ you are allowed to fail
114115
}
115116
// illegal: init? cannot override init
116117
// override init?(name:String, license:Int) {
@@ -196,7 +197,7 @@ class ViewController: UIViewController {
196197
_ = d3
197198
_ = d4
198199

199-
200+
200201
}
201202

202203

bk1ch04p187generics/bk1ch04p187generics/ViewController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func flockTwoTogether<T, U>(f1:T, _ f2:U) {}
4141
let vd : Void = flockTwoTogether("hey", 1)
4242

4343
// illegal in Xcode 7 beta 5!
44+
// still illegal in Swift 2.2, it seems
4445

4546
/*
4647

@@ -143,7 +144,8 @@ func flockTwoTogether6<T1:Flier6, T2:Flier6>(f1:T1, _ f2:T2) {
143144
}
144145

145146
// just testing: this one actually segfaults
146-
// class Dog2<T:Dog2> {}
147+
// but not any more! In Swift 2.2 (Xcode 7.3b) this is fine; not sure when that happened
148+
class Dog2<T:Dog2> {}
147149

148150

149151

bk1ch04p199umbrellaTypes/bk1ch04p199umbrellaTypes/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func flockTwoTogether<T:Flier>(flier:T, _ other:Any) {
3434
}
3535

3636
func typeTester(d:Dog, _ whattype:Dog.Type) {
37-
// if d.dynamicType is whattype { // compile error, "not a type" (i.e. a not a type literal)
37+
// if d.dynamicType is whattype {} // compile error, "not a type" (i.e. a not a type literal)
3838
if d.dynamicType === whattype {
3939
print("yep")
4040
} else {

bk1ch05p239loops/bk1ch05p239loops/ViewController.swift

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,23 @@ class ViewController: UIViewController {
7575
.Number(-1), .Fatal
7676
]
7777
var i = 0
78-
while case let .Message(message) = arr[i++] {
78+
// removed use of i++, deprecated in Swift 2.2, to be removed in Swift 3
79+
while case let .Message(message) = arr[i] {
80+
i = i.successor()
7981
print(message)
8082
}
8183
print(arr)
8284

8385
}
8486

87+
// this _entire_ construct is deprecated in Swift 2.2, to be removed in Swift 3
88+
/*
8589
do {
8690
for var i = 1; i < 6; i++ {
8791
print(i)
8892
}
8993
}
94+
*/
9095

9196
do {
9297
for i in 1...5 {
@@ -96,7 +101,8 @@ class ViewController: UIViewController {
96101

97102
do {
98103
for var i in 1...5 {
99-
i++
104+
// removed use of i++, deprecated in Swift 2.2, to be removed in Swift 3
105+
i = i + 1
100106
print(i)
101107
}
102108
}
@@ -169,6 +175,8 @@ class ViewController: UIViewController {
169175
}
170176

171177

178+
// this _entire_ construct is deprecated in Swift 2.2, to be removed in Swift 3
179+
/*
172180
do {
173181
var i : Int
174182
for i = 1; i < 6; i++ {
@@ -181,7 +189,7 @@ class ViewController: UIViewController {
181189
print(i)
182190
}
183191
}
184-
192+
185193
do {
186194
let tvc = UITableViewCell()
187195
let subview1 = UIView()
@@ -194,12 +202,37 @@ class ViewController: UIViewController {
194202
for v = textField; !(v is UITableViewCell); v = v.superview! {}
195203
print(v)
196204
}
205+
*/
197206

207+
// this is my one example where the loss of C-style for loops is really a pity
208+
// it was great being able to do two things in the prep line
209+
/*
198210
do {
199211
var values = [0.0]
200212
for (var i = 20, direction = 1.0; i < 60; i += 5, direction *= -1) {
201213
values.append( direction * M_PI / Double(i) )
202214
}
215+
print(values) // [0.0, 0.15707963267948966, -0.12566370614359174, 0.10471975511965977, -0.089759790102565518, 0.078539816339744828, -0.069813170079773182, 0.062831853071795868, -0.057119866428905326]
216+
}
217+
*/
218+
219+
// here's one workaround
220+
do {
221+
var values = [0.0]
222+
var direction = 1.0
223+
for i in 20.stride(to: 60, by: 5) {
224+
values.append( direction * M_PI / Double(i) )
225+
direction *= -1
226+
}
227+
print(values)
228+
}
229+
230+
// this is Swiftier and tighter, but a lot harder to understand
231+
do {
232+
var values = [0.0]
233+
for (ix,i) in 20.stride(to: 60, by: 5).enumerate() {
234+
values.append( (ix % 2 == 1 ? -1.0 : 1.0) * M_PI / Double(i) )
235+
}
203236
print(values)
204237
}
205238

bk1ch07p339additionalConfiguration/bk1ch07p339additionalConfiguration/Base.lproj/Main.storyboard

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10102" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10080"/>
55
</dependencies>
66
<scenes>
77
<!--View Controller-->

bk1ch11p467delegation/bk1ch11p467delegation/ColorPicker.xib

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7702" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="10102" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10080"/>
55
</dependencies>
66
<objects>
77
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="ColorPickerController" customModule="bk1ch11p467delegation" customModuleProvider="target">

bk1ch11p472action/bk1ch11p472action/ViewController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ class ViewController: UIViewController {
2929
self.presentViewController(alert, animated: true, completion: nil)
3030
}
3131

32+
// rewritten to avoid use of C-style for loop
3233

3334
@IBAction func showResponderChain(sender: UIResponder) {
34-
var r : UIResponder?
35-
for (r = sender; r != nil; r = r!.nextResponder()) {
35+
var r : UIResponder? = sender
36+
while r != nil {
3637
print(r!)
38+
r = r!.nextResponder()
3739
}
3840
}
3941

bk2ch01p031constraints/ch14p382autoresizing/AppDelegate.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ func dictionaryOfNames(arr:UIView...) -> [String:UIView] {
1818
}
1919

2020
extension NSLayoutConstraint {
21-
class func reportAmbiguity (var v:UIView?) {
21+
class func reportAmbiguity (v:UIView?) {
22+
var v = v
2223
if v == nil {
2324
v = UIApplication.sharedApplication().keyWindow
2425
}
@@ -29,7 +30,8 @@ extension NSLayoutConstraint {
2930
}
3031
}
3132
}
32-
class func listConstraints (var v:UIView?) {
33+
class func listConstraints (v:UIView?) {
34+
var v = v
3335
if v == nil {
3436
v = UIApplication.sharedApplication().keyWindow
3537
}

bk2ch02p051ImageAndTraitCollection/ImageAndTraitCollection/MyView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class MyView: UIView {
1111
}
1212

1313
override func drawRect(rect: CGRect) {
14-
if var im = self.image {
14+
if let im = self.image { // removed "if var", as this will be abolished soon
15+
var im = im
1516
if let asset = self.image.imageAsset {
1617
let tc = self.traitCollection
1718
im = asset.imageWithTraitCollection(tc)

bk2ch02p077drawingInUIView/ch15p427drawingInUIView/MyView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MyView : UIView {
1818
self.opaque = false
1919
}
2020

21-
let which = 6
21+
let which = 1
2222

2323
override func drawRect(rect: CGRect) {
2424
switch which {

bk2ch02p077drawingInUIView/ch15p427drawingInUIView/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ViewController : UIViewController {
3030
NSLayoutConstraint(item: mv, attribute: .CenterY, relatedBy: .Equal, toItem: mv.superview, attribute: .CenterY, multiplier: 1, constant: 0)
3131
)
3232

33-
// return; // comment out to experiment with resizing
33+
return; // comment out to experiment with resizing
3434

3535
delay(0.1) {
3636
mv.bounds.size.height *= 2

bk2ch04p148layerAnimation/ch17p490layerAnimation/ViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class ViewController : UIViewController {
111111
case 9:
112112
var values = [0.0]
113113
// comma is legal in the for block!
114+
//WARNING this will have to be replaced; I already have a replacement ready
114115
for (var i = 20, direction = 1.0; i < 60; i += 5, direction *= -1) { // alternate directions
115116
values.append( direction * M_PI / Double(i) )
116117
}
@@ -142,6 +143,7 @@ class ViewController : UIViewController {
142143

143144
// second animation (waggle)
144145
var values = [0.0]
146+
//WARNING this will have to be replaced; I already have a replacement ready
145147
for (var i = 20, direction = 1.0; i < 60; i += 5, direction *= -1) { // alternate directions
146148
values.append( direction * M_PI / Double(i) )
147149
}

bk2ch04p149keyframeSprite/ch17p495keyframeSprite/ViewController.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ class ViewController : UIViewController {
99

1010
func makeImages () -> [UIImage] {
1111
var arr = [UIImage]()
12-
for (var i = 0; i < 3; i++) {
12+
// replace C for loops by stride
13+
for i in 0.stride(to: 3, by: 1) { // (var i = 0; i < 3; i++) {
1314
UIGraphicsBeginImageContextWithOptions(CGSizeMake(24,24), true, 0)
1415
UIImage(named: "sprites.png")!.drawAtPoint(CGPointMake(CGFloat(-(5+i)*24), -4*24))
1516
let im = UIGraphicsGetImageFromCurrentImageContext()
1617
UIGraphicsEndImageContext()
1718
arr += [im]
1819
}
19-
for (var i = 1; i >= 0; i--) {
20+
for i in 1.stride(through: 0, by: -1) { // (var i = 1; i >= 0; i--) {
2021
UIGraphicsBeginImageContextWithOptions(CGSizeMake(24,24), true, 0)
2122
UIImage(named: "sprites.png")!.drawAtPoint(CGPointMake(CGFloat(-(5+i)*24),-4*24))
2223
let im = UIGraphicsGetImageFromCurrentImageContext()

bk2ch08p466collectionViewFlowLayout2/ch21p748collectionViewFlowLayout2/MyFlowLayout.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class MyFlowLayout : UICollectionViewFlowLayout {
99
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
1010
let arr = super.layoutAttributesForElementsInRect(rect)!
1111
return arr.map {
12-
(var atts) in
12+
atts in // remove (var atts)
13+
var atts = atts
1314
if atts.representedElementKind == nil {
1415
let ip = atts.indexPath
1516
atts = self.layoutAttributesForItemAtIndexPath(ip)!

0 commit comments

Comments
 (0)