回答編集履歴

2

コードの修正

2019/12/02 12:55

投稿

thyda.eiqau
thyda.eiqau

スコア2982

test CHANGED
@@ -1,34 +1,40 @@
1
1
  Swiftは普段あまり触らないので、話半分くらいに見ていただきたいですが……
2
+
3
+
4
+
5
+ Dec 2, 2019 9:55pm追記: Macを触れる環境に来たので修正しました
2
6
 
3
7
 
4
8
 
5
9
  ```swift
6
10
 
11
+
12
+
7
13
  // MARK: -
8
14
 
9
15
  class Car {
10
16
 
11
- var name: String
17
+ var name: String
12
18
 
13
- var wheelsQty: Int
19
+ var wheelsQty: Int
14
20
 
15
21
 
16
22
 
17
- init(name: String) {
23
+ init(name: String) {
18
24
 
19
- self.name = name
25
+ self.name = name
20
26
 
21
- self.wheelsQty = 4
27
+ self.wheelsQty = 4
22
28
 
23
- }
29
+ }
24
30
 
25
-
26
31
 
27
- func run() {
28
32
 
29
- print("Go ahead (self.name), zoom-zoom!")
33
+ func run() {
30
34
 
35
+ print("Go ahead (self.name), zoom-zoom!")
36
+
31
- }
37
+ }
32
38
 
33
39
  }
34
40
 
@@ -38,19 +44,19 @@
38
44
 
39
45
  class Bike: Car {
40
46
 
41
- let hasSidecar: Bool
47
+ var hasSidecar: Bool
42
48
 
43
-
44
49
 
45
- init(name: String, hasSidecar: Bool) {
46
50
 
47
- self = super.init(name: name)
51
+ init(name: String, hasSidecar: Bool) {
48
52
 
49
- self.hasSidecar = hasSidecar
53
+ self.hasSidecar = hasSidecar
50
54
 
51
- self.wheelsQty = 2
55
+ super.init(name: name)
52
56
 
57
+ self.wheelsQty = 2
58
+
53
- }
59
+ }
54
60
 
55
61
  }
56
62
 
@@ -60,25 +66,25 @@
60
66
 
61
67
  class Truck: Car {
62
68
 
63
- let specialSigns: Array<String>
69
+ var specialSigns: Array<String>
64
70
 
65
-
66
71
 
67
- init(name: String, specialSigns: Array<String>) {
68
72
 
69
- self = super.init(name: name)
73
+ init(name: String, specialSigns: Array<String>) {
70
74
 
71
- self.specialSigns: specialSigns
75
+ self.specialSigns = specialSigns
72
76
 
73
- }
77
+ super.init(name: name)
74
78
 
75
-
79
+ }
76
80
 
77
- override run() {
78
81
 
79
- print("Vrrr, Vrrrr, Vrrrrrrom!!!!")
80
82
 
83
+ override func run() {
84
+
85
+ print("Vrrr, Vrrrr, Vrrrrrrom!!!!")
86
+
81
- }
87
+ }
82
88
 
83
89
  }
84
90
 
@@ -86,55 +92,39 @@
86
92
 
87
93
  // MARK: -
88
94
 
89
- class MainViewController: UIViewController {
95
+ class ViewController: UIViewController {
90
96
 
91
- let touring = new Bike(name: "Touring", hasSidecar: true)
97
+ required init?(coder: NSCoder) {
92
98
 
93
- let hayabusa: Car = new Bike(name: "Hayabusa", hasSidecar: true)
99
+ super.init(coder: coder)
94
100
 
95
- let canter = new Truck(name: "CANTER", specialSigns: ["危", "毒"])
96
-
97
- let elf: Car = new Truck(name: "ELF", specialSigns: [])
98
-
99
- var myFavoriteCar = some Car
101
+ }
100
-
101
-
102
-
103
- override func viewDidLoad() {
104
-
105
- print("(touring.hasSidecar)") // -> true
106
-
107
- print("(hayabusa.hasSidecar)") // -> ERROR
108
102
 
109
103
 
110
104
 
111
- touring.run() // -> Go ahead Touring, zoom-zoom!
105
+ let touring = Bike(name: "Touring", hasSidecar: true)
112
106
 
113
- hayabusa.run() // -> Go ahead Hayabusa, zoom-zoom!
107
+ let hayabusa: Car = Bike(name: "Hayabusa", hasSidecar: true)
114
108
 
115
- canter.run() // -> Vrrr, Vrrrr, Vrrrrrrom!!!!
109
+ let canter = Truck(name: "CANTER", specialSigns: ["危", "毒"])
116
110
 
117
- elf.run() // -> Go ahead ELF, zoom-zoom!
111
+ let elf: Car = Truck(name: "ELF", specialSigns: [])
118
112
 
119
113
 
120
114
 
121
- self.myFavoriteCar = touring
115
+ override func viewDidLoad() {
122
116
 
123
- printCurrentFavorite() // -> My favorite is Touring, this is a Bike
117
+ print("(touring.hasSidecar)") // -> true
124
118
 
125
- self.myFavoriteCar = hayabusa
119
+ print("(hayabusa.hasSidecar)") // -> ERROR
126
120
 
127
- printCurrentFavorite() // -> My favorite is Hayabusa, this is a Car
128
121
 
129
- }
130
122
 
131
-
123
+ print("(canter.specialSigns)") // -> ["危", "毒"]
132
124
 
133
- func printCurrentFavorite() {
125
+ print("(elf.specialSigns)") // -> ERROR
134
126
 
135
- print("My favorite is (self.myFavoriteCar.name), this is a (type(of: self.myFavoriteCar))")
136
-
137
- }
127
+ }
138
128
 
139
129
  }
140
130
 

1

typo修正とコメント追加

2019/12/02 12:55

投稿

thyda.eiqau
thyda.eiqau

スコア2982

test CHANGED
@@ -1,3 +1,7 @@
1
+ Swiftは普段あまり触らないので、話半分くらいに見ていただきたいですが……
2
+
3
+
4
+
1
5
  ```swift
2
6
 
3
7
  // MARK: -
@@ -92,7 +96,7 @@
92
96
 
93
97
  let elf: Car = new Truck(name: "ELF", specialSigns: [])
94
98
 
95
- var myFavoriteCar: Car?
99
+ var myFavoriteCar = some Car
96
100
 
97
101
 
98
102
 
@@ -128,7 +132,7 @@
128
132
 
129
133
  func printCurrentFavorite() {
130
134
 
131
- print("My favorite is (self.myFavoriteCar.name), this is a (type(of: a))")
135
+ print("My favorite is (self.myFavoriteCar.name), this is a (type(of: self.myFavoriteCar))")
132
136
 
133
137
  }
134
138