-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilecreator.swift
More file actions
59 lines (51 loc) · 1.83 KB
/
Filecreator.swift
File metadata and controls
59 lines (51 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// FileManager.swift
// CRC_iOS
//
// Created by 우예지 on 2021/06/25.
//
import UIKit
class FileCreator {
let ad = UIApplication.shared.delegate as? AppDelegate
let fileManager = FileManager()
// Creates folder
func setFolderDirectory() -> URL {
// create directory
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
let directoryURL = documentsURL.appendingPathComponent("HCILabData")
do {
try fileManager.createDirectory(atPath: directoryURL.path, withIntermediateDirectories: false, attributes: nil)
} catch let error as NSError {
print("Error creating directory: \(error.localizedDescription)")
}
return directoryURL
}
// Creates file
func createFileURL(transportation: String, sensor: String, directoryURL: URL) -> URL {
// create date string
let formattedDate = dateManager(type: "file")
// create file directory
let fileURL = directoryURL.appendingPathComponent(formattedDate + "_" + transportation + "_" + sensor + ".csv")
return fileURL
}
// Writes in file
func writeInFile(csvString: String, fileURL: URL) -> Void {
let text = NSString(string: csvString)
do {
try text.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8.rawValue)
} catch let e {
print(e.localizedDescription)
}
}
func dateManager(type: String) -> String {
let date = Date()
let format = DateFormatter()
if type == "file" {
format.dateFormat = "yyyy_MM_dd_HH_mm_ss"
} else if type == "data" {
format.dateFormat = "yyyy,MM,dd,HH,mm,ss,"
}
let formattedDate = format.string(from: date)
return formattedDate
}
}