回答編集履歴
3
答えを追記
answer
CHANGED
@@ -1,7 +1,56 @@
|
|
1
|
-
まだ途中ですが、どうもCollection継承だとgetしかできないみたいなので、
|
1
|
+
~~まだ途中ですが、~~どうもCollection継承だとgetしかできないみたいなので、
|
2
|
-
setも利用するにはMutableCollectionを継承する
|
2
|
+
# setも利用するにはMutableCollectionを継承する
|
3
|
+
に変更しないといけないみたいです。
|
3
4
|
|
4
5
|
そのように変更し、試行錯誤中です。
|
5
|
-
書き換え途中ですが、やはり詰まってしまいました。
|
6
|
+
~~書き換え途中ですが、やはり詰まってしまいました。~~
|
6
7
|
|
8
|
+
# stackOverflowでいいサンプル
|
9
|
+
があり、最終的に以下のコードで実現できました。
|
10
|
+
|
11
|
+
var cells = TempCells()をより構造的に強固なletで定義したかったですが、
|
12
|
+
そうすると、メンバ変数にできないので、無理みたいなので、これで解決とします。
|
13
|
+
|
14
|
+
```Swift
|
15
|
+
import UIKit
|
16
|
+
|
17
|
+
class ViewController: UIViewController {
|
18
|
+
|
19
|
+
override func viewDidLoad() {
|
20
|
+
super.viewDidLoad()
|
21
|
+
// Do any additional setup after loading the view.
|
22
|
+
|
23
|
+
var cells = TempCells()
|
24
|
+
print("old", cells.description)
|
25
|
+
cells.rewriteName(firstName: "大阪", lastName: "太郎")
|
26
|
+
print("new", cells.description)
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
struct TempCells: MutableCollection {
|
31
|
+
var cells: [PresentCell] = []
|
32
|
+
|
33
|
+
init() {
|
34
|
+
self.cells = [PresentCell.name(NameData(firstName: "東京", lastName: "太郎")),
|
35
|
+
PresentCell.birthday(DateData(date: Date().addingTimeInterval(-60*60*24*365*20).timeIntervalSince1970)),
|
36
|
+
PresentCell.university(SchoolData(university: "東京大学"))]
|
37
|
+
}
|
38
|
+
|
39
|
+
var description: String { return cells.description }
|
40
|
+
var startIndex: Int { return 0 }
|
41
|
+
var endIndex: Int { return cells.count }
|
42
|
+
|
43
|
+
func index(after i: Int) -> Int { return i + 1 }
|
44
|
+
|
45
|
+
subscript(position: Int) -> PresentCell {
|
46
|
+
get { return cells[position] }
|
47
|
+
set(newValue) { cells[position] = newValue }
|
48
|
+
}
|
49
|
+
|
50
|
+
mutating func rewriteName(firstName: String, lastName: String) {
|
51
|
+
self[0] = PresentCell.name(NameData(firstName: firstName, lastName: lastName))
|
52
|
+
}
|
53
|
+
}
|
54
|
+
```
|
55
|
+
|
7
|
-

|
2
追加
answer
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
まだ途中ですが、どうもCollection継承だとgetしかできないみたいなので、
|
2
2
|
setも利用するにはMutableCollectionを継承するに変更しないといけないみたいです。
|
3
3
|
|
4
|
-
そのように変更し、試行錯誤中です。
|
4
|
+
そのように変更し、試行錯誤中です。
|
5
|
+
書き換え途中ですが、やはり詰まってしまいました。
|
6
|
+
|
7
|
+

|
1
追記
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
まだ途中ですが、どうもCollection継承だとgetしかできないみたいなので、
|
2
|
-
MutableCollection継承に変更
|
2
|
+
setも利用するにはMutableCollectionを継承するに変更しないといけないみたいです。
|
3
3
|
|
4
|
-
試行錯誤中です。
|
4
|
+
そのように変更し、試行錯誤中です。
|