回答編集履歴
1
修正
test
CHANGED
@@ -4,6 +4,12 @@
|
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
+
CSVに改行を入れている場合
|
8
|
+
|
9
|
+
---
|
10
|
+
|
11
|
+
|
12
|
+
|
7
13
|
```swift
|
8
14
|
|
9
15
|
import UIKit
|
@@ -93,3 +99,111 @@
|
|
93
99
|
6,7,8,9,10
|
94
100
|
|
95
101
|
```
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
CSVに改行を入れていない場合
|
106
|
+
|
107
|
+
---
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
```swift
|
112
|
+
|
113
|
+
import UIKit
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
class ViewController: UIViewController {
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
var str: Array<Array<String>> = []
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
override func viewDidLoad() {
|
126
|
+
|
127
|
+
super.viewDidLoad()
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
self.view.backgroundColor = UIColor.black
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
// CSV読み込み
|
136
|
+
|
137
|
+
if let csvPath = Bundle.main.path(forResource: "file", ofType: "csv") {
|
138
|
+
|
139
|
+
do {
|
140
|
+
|
141
|
+
var csvString = try NSString(contentsOfFile: csvPath, encoding: String.Encoding.utf8.rawValue) as String
|
142
|
+
|
143
|
+
csvString = csvString.replacingOccurrences(of: "\r\n", with: "")
|
144
|
+
|
145
|
+
let array:Array = csvString.components(separatedBy: ",")
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
var items = [String]()
|
150
|
+
|
151
|
+
array.enumerated().forEach { (index, value) in
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
items.append(value)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
if (index + 1) % 5 == 0 {
|
160
|
+
|
161
|
+
str.append(items)
|
162
|
+
|
163
|
+
items = [String]()
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
} catch {
|
170
|
+
|
171
|
+
// エラー
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
print(str)
|
180
|
+
|
181
|
+
print(str[0][2])
|
182
|
+
|
183
|
+
//=> 3
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
print(str[1][4])
|
188
|
+
|
189
|
+
//=> 10
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
```
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
☆ file.csv
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
```CSV
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
1,2,3,4,5,6,7,8,9,10
|
208
|
+
|
209
|
+
```
|