Skip to content

Commit 9e18f6a

Browse files
committed
more selector syntax examples
1 parent 42f90f2 commit 9e18f6a

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

bk1ch02p044functionAsValue/bk1ch02p044functionAsValue/ViewController.swift

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,88 @@ func imageOfSize(size:CGSize, _ whatToDraw:() -> ()) -> UIImage {
1313
return result
1414
}
1515

16+
class Cat {
17+
func purr () {
18+
19+
}
20+
}
1621

22+
class Dog {
23+
let cat = Cat()
24+
func bark() {
25+
print("woof")
26+
}
27+
func bark(loudly:Bool) {
28+
if loudly {
29+
print("WOOF")
30+
} else {
31+
self.bark()
32+
}
33+
}
34+
func test() {
35+
// let barkFunction = bark
36+
let barkFunction1 = bark(_:)
37+
let barkFunction2 = bark as Void -> Void
38+
let barkFunction3 = bark as Bool -> Void
39+
let barkFunction4 : Bool -> Void = bark
40+
let barkFunction5 = self.bark(_:)
41+
42+
let barkFunction6 = self.dynamicType.bark(_:)
43+
let barkFunction7 = Dog.bark(_:)
44+
45+
_ = barkFunction1
46+
_ = barkFunction2
47+
_ = barkFunction3
48+
_ = barkFunction4
49+
_ = barkFunction5
50+
_ = barkFunction6
51+
_ = barkFunction7
52+
53+
let f = {
54+
// return bark(_:) // error
55+
return self.bark(_:)
56+
}
57+
_ = f
58+
59+
let purrFunction1 = cat.purr
60+
let purrFunction2 = self.cat.purr
61+
let purrFunction3 = Cat.purr
62+
63+
_ = purrFunction1
64+
_ = purrFunction2
65+
_ = purrFunction3
66+
}
67+
}
68+
69+
class NoisyDog : Dog {
70+
func test2() {
71+
let barkFunction1 = bark(_:)
72+
let barkFunction2 = self.bark(_:)
73+
let barkFunction3 = super.bark(_:)
74+
75+
_ = barkFunction1
76+
_ = barkFunction2
77+
_ = barkFunction3
78+
79+
}
80+
}
81+
82+
class Dog2 {
83+
func bark() {
84+
85+
}
86+
func bark(loudly:Bool) {
87+
88+
}
89+
func bark(times:Int) {
90+
91+
}
92+
func test() {
93+
// let barkFunction = bark(_:)
94+
let barkFunction = bark(_:) as Int -> Void
95+
_ = barkFunction
96+
}
97+
}
1798

1899

19100
class ViewController: UIViewController {
@@ -24,7 +105,6 @@ class ViewController: UIViewController {
24105
override func viewDidLoad() {
25106
super.viewDidLoad()
26107

27-
28108
func whatToDo() {
29109
print("I did it")
30110
}
@@ -46,6 +126,9 @@ class ViewController: UIViewController {
46126

47127
}
48128

129+
130+
131+
49132
@IBAction func moveMyButton (sender:AnyObject!) {
50133
func whatToAnimate() { // self.myButton is a button in the interface
51134
self.myButton.frame.origin.y += 20
@@ -84,6 +167,8 @@ class ViewController: UIViewController {
84167
let f3 = ViewController.moveMyButton
85168
let ff3 = ViewController.moveMyButton(_:)
86169

170+
// let v = viewDidLoad
171+
87172
f(self.myButton)
88173
ff(self.myButton)
89174
f2(self.myButton)

0 commit comments

Comments
 (0)