I am looking for a way to create separate files. For example: I have an application which has a form such as name, date etc. and when I press save, I want to save all the information in that form of the user into a file. Each time the form is filled in, I want a new file to be created for each user. The file will be saved in the documents directory of the application. The name of the file should come from the 'name' textfield of the form. I have looked at ways to create files in swift but the filename is always hardcoded in the path url and every time the form is filled out the information gets saved on the same file. I don't want to do this. Any solution or advice is appreciated, I can't seem to find any solution to this online (only how to create a file with a hardcoded file name, which I am already able to do).
import UIKit
class FormViewController: UIViewController {
@IBOutlet weak var organdonationno: UITextField!
@IBOutlet weak var donorage: UITextField!
@IBOutlet weak var snodname: UITextField!
@IBOutlet weak var donorshospital: UITextField!
@IBOutlet weak var date: UITextField!
@IBOutlet weak var ID: UITextField!
var fileMgr: FileManager = FileManager.default
var docsDir: String?
var dataFile: String?
override func viewDidLoad() {
super.viewDidLoad()
let filemgr = FileManager.default
let dirPaths = filemgr.urls(for: .documentDirectory, in: .userDomainMask)
dataFile = dirPaths[0].appendingPathComponent("path").path
if filemgr.fileExists(atPath: dataFile!) {
let databuffer1 = filemgr.contents(atPath: dataFile!)
let databuffer2 = filemgr.contents(atPath: dataFile!)
let databuffer3 = filemgr.contents(atPath: dataFile!)
let databuffer4 = filemgr.contents(atPath: dataFile!)
let databuffer5 = filemgr.contents(atPath: dataFile!)
let datastring1 = NSString(data: databuffer1!, encoding: String.Encoding.utf8.rawValue)
let datastring2 = NSString(data: databuffer2!, encoding: String.Encoding.utf8.rawValue)
let datastring3 = NSString(data: databuffer3!, encoding: String.Encoding.utf8.rawValue)
let datastring4 = NSString(data: databuffer4!, encoding: String.Encoding.utf8.rawValue)
let datastring5 = NSString(data: databuffer5!, encoding: String.Encoding.utf8.rawValue)
organdonationno.text = datastring1 as? String
donorage.text = datastring2 as? String
snodname.text = datastring3 as? String
donorshospital.text = datastring4 as? String
date.text = datastring5 as? String
}
// Do any additional setup after loading the view.
}
@IBAction func savdata(_ sender: Any) {
let databuffer1 = (organdonationno.text)!.data(using: String.Encoding.utf8)
let databuffer2 = (donorage.text)!.data(using: String.Encoding.utf8)
let databuffer3 = (snodname.text)!.data(using: String.Encoding.utf8)
let databuffer4 = (donorshospital.text)!.data(using: String.Encoding.utf8)
let databuffer5 = (date.text)!.data(using: String.Encoding.utf8)
fileMgr.createFile(atPath: dataFile!, contents: databuffer1, attributes: nil)
fileMgr.createFile(atPath: dataFile!, contents: databuffer2, attributes: nil)
fileMgr.createFile(atPath: dataFile!, contents: databuffer3, attributes: nil)
fileMgr.createFile(atPath: dataFile!, contents: databuffer4, attributes: nil)
fileMgr.createFile(atPath: dataFile!, contents: databuffer5, attributes: nil)
}
}