// start by reading data from CSV var rawPoints:[(date: Date, value: Double)] = [] let file = "AAPL 1980-12-12 to 2017-10-13.csv" let basePath = Bundle.main.resourceURL let fileURL = basePath?.appendingPathComponent(file) let formatter = ISO8601DateFormatter() formatter.formatOptions = [.withFullDate, .withDashSeparatorInDate] do { let text = try String(contentsOf: fileURL!, encoding: .utf8) let lines = text.split(separator: "\n") for line in lines { let values = line.split(separator: ";") if values.count != 2 { continue } let dateString = values[0] let valueString = values[1] let date = formatter.date(from: String(dateString)) let value = Double(valueString) if let date = date, let value = value { rawPoints.append((date, value)) } } } catch { /* error handling here */ } // Ok, now let's rebase from first date //let firstDate = rawPoints[0].date var points:[StockPoint] = [] var numPoints = 0.0 for point in rawPoints { points.append(StockPoint( //when: point.date.timeIntervalSince(firstDate) / (24*60*60), when: numPoints, value: point.value )) numPoints += 1 } self.points = points
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.