Skip to content

Commit 88fbb81

Browse files
committed
more preparation for Swift 2.2, mostly selector syntax; various other notes to self as well
1 parent 848b3b3 commit 88fbb81

File tree

29 files changed

+138
-67
lines changed

29 files changed

+138
-67
lines changed

bk1ch02p036defaultParameters/bk1ch02p036defaultParameters/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="10112" 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="10083"/>
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="bk1ch02p036defaultParameters" customModuleProvider="target" sceneMemberID="viewController">
1111
<layoutGuides>
1212
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
1313
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>

bk1ch02p036defaultParameters/bk1ch02p036defaultParameters/ViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ class ViewController: UIViewController {
4848

4949
sayStrings("hey", "ho", "nonny nonny no")
5050

51-
sayStrings("Mannie", "Moe", "Jack", times:3)
51+
sayStrings("Manny", "Moe", "Jack", times:3)
5252

5353
// print is now variadic
5454

55-
print("Mannie", 3, true) // Mannie 3 true
55+
print("Manny", 3, true) // Manny 3 true
5656

57-
print("Mannie", "Moe", separator:", ", terminator: ", ")
57+
print("Manny", "Moe", separator:", ", terminator: ", ")
5858
print("Jack")
5959

6060
say("hi", times:3, loudly:true)

bk1ch02p044functionAsValue/bk1ch02p044functionAsValue/ViewController.swift

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class ViewController: UIViewController {
2929
print("I did it")
3030
}
3131
doThis(whatToDo)
32-
33-
32+
3433

3534
func drawing() {
3635
let p = UIBezierPath(
@@ -48,8 +47,6 @@ class ViewController: UIViewController {
4847
}
4948

5049
@IBAction func moveMyButton (sender:AnyObject!) {
51-
52-
5350
func whatToAnimate() { // self.myButton is a button in the interface
5451
self.myButton.frame.origin.y += 20
5552
}
@@ -58,8 +55,6 @@ class ViewController: UIViewController {
5855
}
5956
UIView.animateWithDuration(
6057
0.4, animations: whatToAnimate, completion: whatToDoLater)
61-
62-
6358
}
6459

6560
func test() {
@@ -68,10 +63,52 @@ class ViewController: UIViewController {
6863
print("I finished!")
6964
}
7065
self.presentViewController(vc, animated:true, completion:whatToDoLater)
66+
7167
}
7268

69+
func playingWithFunctionSpecifierSyntax() {
70+
/*
71+
Illustrating ways to refer to a function as a value in Swift 2.2 and later.
72+
You can express the signature, e.g. as a way of disambiguating in case of overload.
73+
This will also turn out to be essential when using the new #selector syntax.
74+
You can precede with an explicit instance.
75+
You can precede with an explicit class. This is because an instance method
76+
is really a class method; as the calling example shows, this is not the same thing.
77+
But it will turn out that this can be used the same way in #selector.
78+
*/
79+
80+
let f = moveMyButton
81+
let ff = moveMyButton(_:)
82+
let f2 = self.moveMyButton
83+
let ff2 = self.moveMyButton(_:)
84+
let f3 = ViewController.moveMyButton
85+
let ff3 = ViewController.moveMyButton(_:)
86+
87+
f(self.myButton)
88+
ff(self.myButton)
89+
f2(self.myButton)
90+
ff2(self.myButton)
91+
f3(self)(self.myButton)
92+
ff3(self)(self.myButton)
93+
}
7394

74-
95+
// I should probably now discuss #selector syntax at this point in the book
96+
97+
func testSelectorSyntax() {
98+
let b = UIButton(type: .System)
99+
b.addTarget(self, action: #selector(doButton), forControlEvents: .TouchUpInside)
100+
// or:
101+
b.addTarget(self, action: #selector(doButton(_:)), forControlEvents: .TouchUpInside)
102+
// or:
103+
b.addTarget(self, action: #selector(ViewController.doButton(_:)), forControlEvents: .TouchUpInside)
104+
// and so on; but the point is, you just need to provide enough info ...
105+
// ... so that the compiler can resolve this method reference for you
106+
// and it will then form the actual Selector for you! no more "unrecognized selector"!
107+
}
108+
109+
func doButton(sender:AnyObject) { // must actually exist, or none of the above will compile
110+
111+
}
75112

76113
}
77114

bk1ch03p095tuple/bk1ch03p095tuple/ViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class ViewController: UIViewController {
105105

106106

107107
// parameter list in function call is actually a tuple
108+
// however, new in Swift 2.2, these two calls now get a warning:
109+
// "Passing 2 arguments to a callee as a single tuple value is deprecated"
110+
// so is this "feature" going away? If so, you could _store_ as a tuple ...
111+
// ...but _pass_ as individual arguments
108112

109113
do {
110114
let tuple = (1,2)

bk1ch04p142valueTypesAndReferenceTypes/bk1ch04p142valueTypesAndReferenceTypes/ViewController.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,24 @@ func digitChanger(d:Digit) {
2828
}
2929
*/
3030

31+
// currently works, but deprecated in Swift 2.2 and will be removed in Swift 3
32+
// the example exactly illustrates the reason: you probably _think_ you are changing the original Digit object...
33+
// but you're just changing a copy
34+
// hence you now have two options: either redeclare var d = d to make local d mutable...
35+
// or, if you really intended to change the original Digit, use inout
36+
37+
/*
38+
3139
func digitChanger(var d:Digit) {
3240
d.number = 42
3341
}
42+
43+
*/
44+
45+
func digitChanger(d:Digit) {
46+
var d = d
47+
d.number = 42
48+
}
3449

3550
func dogChanger(d:Dog) {
3651
d.name = "Rover"

bk1ch04p194whereClauses2/bk1ch04p194whereClauses2/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class ViewController: UIViewController {
8888
s.appendContentsOf(["!" as Character])
8989
print(s) // "hello world"
9090

91-
var arr = ["mannie", "moe"]
91+
var arr = ["manny", "moe"]
9292
arr.appendContentsOf(["jack"])
9393
// arr.appendContentsOf([1]) // nope
9494

bk1ch04p196extensions/bk1ch04p196extensions/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="10112" 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="10083"/>
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="bk1ch04p196extensions" customModuleProvider="target" sceneMemberID="viewController">
1111
<layoutGuides>
1212
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
1313
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>

bk1ch04p204arrays/bk1ch04p204collections/Thing.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
@implementation Pep
66

77
- (NSArray*) boys {
8-
return @[@"Mannie", @"Moe", @"Jack"];
8+
return @[@"Manny", @"Moe", @"Jack"];
99
}
1010
- (NSArray*) boysGood {
11-
return @[@"Mannie", @"Moe", @"Jack"];
11+
return @[@"Manny", @"Moe", @"Jack"];
1212
}
1313

1414

bk1ch04p204arrays/bk1ch04p204collections/ViewController.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ class ViewController: UIViewController {
3939
let objs = [1, "howdy"]
4040
// let arrr = [Insect(), Bird()] // compile error
4141
let arr : [Flier] = [Insect(), Bird()]
42+
43+
do {
44+
let arr2 : [Flier] = [Insect()]
45+
// WARNING next line is legal (compiles) but you'll crash at runtime
46+
// can't use array-casting to cast down from protocol type to adopter type
47+
// let arr3 = arr2 as! [Insect]
48+
_ = arr2
49+
}
50+
51+
do {
52+
let arr2 : [Flier] = [Insect()]
53+
// instead of above, to cast down, must cast individual elements down
54+
let arr3 = arr2.map{$0 as! Insect}
55+
_ = arr2
56+
_ = arr3
57+
}
58+
4259
let rs = Array(1...3)
4360
print(rs)
4461
let chars = Array("howdy".characters)
@@ -296,8 +313,15 @@ class ViewController: UIViewController {
296313

297314
let arr2 = [[1,2], [3,4], [5,6], 7]
298315
let arr3 = arr2.flatMap {$0}
299-
print(arr3) // I honestly don't understand this one
300-
316+
print(arr3)
317+
}
318+
319+
// flatMap has another use that I really should talk about:
320+
// it unwraps Optionals safely while eliminating nils
321+
do {
322+
let arr : [String?] = ["Manny", nil, nil, "Moe", nil, "Jack", nil]
323+
let arr2 = arr.flatMap{$0}
324+
print(arr2)
301325
}
302326

303327
do {

bk1ch04p218dictionaries/bk1ch04p218dictionaries/ViewController.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ class ViewController: UIViewController {
9595
]
9696

9797
let nc = NSNotificationCenter.defaultCenter()
98-
nc.addObserver(self, selector: "notificationArrived:", name: "test", object: nil)
98+
// Cool and long-awaited new feature of Swift 2.2: no more string selectors
99+
// This means the compiler will form the actual selector for you
100+
// You don't even have to get it totally right! Here, I've used the bare name...
101+
// ...but Swift will still form the selector correctly for me
102+
// In other words, any valid reference to the method will do
103+
nc.addObserver(self, selector:#selector(notificationArrived), name: "test", object: nil)
99104
nc.postNotificationName("test", object: self, userInfo: ["junk":"nonsense"])
100105
nc.postNotificationName("test", object: self, userInfo: ["progress":"nonsense"])
101106
nc.postNotificationName("test", object: self, userInfo: ["progress":3])
@@ -126,6 +131,8 @@ class ViewController: UIViewController {
126131
if prog != nil {
127132
self.progress = prog!.doubleValue
128133
print("at last! \(self.progress)")
134+
} else {
135+
print("invalid notification")
129136
}
130137
}
131138

bk1ch05p227branching/bk1ch05p227branching/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ViewController: UIViewController {
4747

4848

4949
let nc = NSNotificationCenter.defaultCenter()
50-
nc.addObserver(self, selector: "notificationArrived:", name: "test", object: nil)
50+
nc.addObserver(self, selector: #selector(notificationArrived), name: "test", object: nil)
5151
nc.postNotificationName("test", object: self, userInfo: ["junk":"nonsense"])
5252
nc.postNotificationName("test", object: self, userInfo: ["progress":"nonsense"])
5353
nc.postNotificationName("test", object: self, userInfo: ["progress":3])

bk1ch05p230switch/bk1ch05p230switch/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ class ViewController: UIViewController {
291291

292292

293293
let nc = NSNotificationCenter.defaultCenter()
294-
nc.addObserver(self, selector: "notificationArrived:", name: "test", object: nil)
294+
nc.addObserver(self, selector: #selector(notificationArrived), name: "test", object: nil)
295295
nc.postNotificationName("test", object: self, userInfo: ["junk":"nonsense"])
296296
nc.postNotificationName("test", object: self, userInfo: ["progress":"nonsense"])
297297
nc.postNotificationName("test", object: self, userInfo: ["progress":3])

bk1ch05p239loops/bk1ch05p239loops/Thing.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@implementation Pep
66

77
- (NSArray*) boys {
8-
return @[@"Mannie", @"Moe", @"Jack"];
8+
return @[@"Manny", @"Moe", @"Jack"];
99
}
1010

1111
@end

bk1ch10p418subclassing/bk1ch10p418subclassing/ViewController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ class ViewController: UIViewController {
1919
override func viewDidLoad() {
2020
super.viewDidLoad()
2121

22+
// ignore warnings (new in Swift 2.2)
23+
2224
let mc = MyClass()
23-
if mc.respondsToSelector("woohoo") {
25+
if mc.respondsToSelector(Selector("woohoo")) {
2426
print("here1")
2527
(mc as AnyObject).woohoo()
2628
}
2729
let mc2 = MyClass2()
28-
if mc2.respondsToSelector("woohoo") {
30+
if mc2.respondsToSelector(Selector("woohoo")) {
2931
print("here2")
3032
(mc2 as AnyObject).woohoo()
3133
}

bk1ch10p428foundationClasses/bk1ch10p428foundationClasses/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1616

1717
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
1818
// Override point for customization after application launch.
19-
NSNotificationCenter.defaultCenter().addObserver(self, selector: "doProg:", name: "prog", object: self)
19+
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(doProg), name: "prog", object: self)
2020
NSNotificationCenter.defaultCenter().postNotificationName("prog", object: self, userInfo: ["progress":4.2])
2121
return true
2222
}

bk1ch11p462notifications/bk1ch11p462notifications/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1111
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
1212

1313
NSNotificationCenter.defaultCenter().addObserver(self,
14-
selector: "cardTapped:",
14+
selector: #selector(cardTapped),
1515
name: "cardTapped",
1616
object: nil)
1717

bk1ch11p462notifications/bk1ch11p462notifications/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ViewController: UIViewController {
2020
case 1:
2121

2222
NSNotificationCenter.defaultCenter().addObserver(self,
23-
selector: "nowPlayingItemChanged:",
23+
selector: #selector(nowPlayingItemChanged),
2424
name: MPMusicPlayerControllerNowPlayingItemDidChangeNotification,
2525
object: nil)
2626

bk1ch11p472action/bk1ch11p472action/ViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ class ViewController: UIViewController {
1010
super.viewDidLoad()
1111

1212
self.button.addTarget(self,
13-
action: "buttonPressed:",
13+
action: #selector(buttonPressed),
1414
forControlEvents: .TouchUpInside)
1515

1616
self.button2.addTarget(nil, // nil-targeted
17-
action: "buttonPressed:",
17+
action: #selector(buttonPressed),
1818
forControlEvents: .TouchUpInside)
1919

2020
// third button is configured as nil-targeted in nib

bk1ch12p490autoreleasepool/bk1ch12p490autoreleasepool/ViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class ViewController: UIViewController {
1515
}
1616
}
1717

18+
// ignore warnings
19+
1820
func test() {
1921
let path = NSBundle.mainBundle().pathForResource("001", ofType: "png")!
2022
for j in 0 ..< 50 {

bk1ch12p498timerLeaker/ch12p325NotificationLeaker/FlipsideViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class FlipsideViewController: UIViewController {
1515
override func viewWillAppear(animated: Bool) {
1616
super.viewWillAppear(animated)
1717
print("starting timer")
18-
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "dummy:", userInfo: nil, repeats: true)
18+
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(dummy), userInfo: nil, repeats: true)
1919
self.timer.tolerance = 0.1
2020
}
2121

bk1ch14Appendix/Appendix/Thing.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ - (nullable NSString*) goodMethod: (NSString*) s {
1919
}
2020

2121
- (NSArray<NSString*>*) pepBoys {
22-
return @[@"Mannie", @"Moe", @"Jack"];
22+
return @[@"Manny", @"Moe", @"Jack"];
2323
}
2424

2525
- (void) justTesting {

bk1ch14Appendix/Appendix/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class ViewController: UIViewController {
148148
do {
149149
// hold my beer and watch _this_!
150150

151-
let arr = ["Manniez", "Moey", "Jackx"]
151+
let arr = ["Mannyz", "Moey", "Jackx"]
152152
func sortByLastCharacter(s1:AnyObject,
153153
_ s2:AnyObject, _ context: UnsafeMutablePointer<Void>) -> Int {
154154
let c1 = (s1 as! String).characters.last

bk2ch01p038evenDistribution4/bk2ch01p038evenDistribution3/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="9059" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10112" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9049"/>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10083"/>
55
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
66
</dependencies>
77
<scenes>

0 commit comments

Comments
 (0)