title: Creating and writing multiple lines of text to a text file (CSV)?
body: <p>I'm trying to create a way for a user to sign in and store their username and password in a text file. This is for a school project, so it's not stored securely and I don't think I'll have time to set up/learn how to serialize the data anyways. 
  The main part I'm having issues with is trying to write the data as a CSV to later split the user's scores into an array based off of the data it received from the text file.</p>
  
  <p>I've tried a different method of writing to the text file which was:<br>
  <code>writeString.data(using: String.Encoding.utf8)?.write(to: fileURL, options: Data.WritingOptions.withoutOverwriting)</code> But this didn't seem to work for me</p>
  
  <pre><code>struct UserAccount: Codable {
          var username: String
          var password: String
          var scores: [Int]
      }
  
  var user = UserAccount(username: "", password: "", scores: [0])
  
  func writeTextFile(_ user: UserAccount) {
      //Creating text file to read and write user's data (username, password, and score values)
      let fileName = "UserDataQuizApp"
      let dir = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
  
      do {
          let fileManager = FileManager.default
  
          if fileManager.fileExists(atPath: "UserDataQuizApp") {
              try fileManager.removeItem(atPath: fileName)
          }
      } catch {print("error when deleting \(fileName)") }
  
      // If the directory was found, we write a file to it and read it back
      if let fileURL = dir?.appendingPathComponent(fileName).appendingPathExtension("txt") {
          print("The path is: \(fileURL)")
  
          do {
              try user.username.write(to: fileURL, atomically: false, encoding: .utf8)
              try ",".write(to: fileURL, atomically: false, encoding: .utf8)
              try user.password.write(to: fileURL, atomically: false, encoding: .utf8)
              for i in 0 ... user.scores.count {
                  try ",".write(to: fileURL, atomically: true, encoding: .utf8)
                  try String(user.scores[i]).write(to: fileURL, atomically: true, encoding: .utf8)
              }
          } catch {
              print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
          }
      }
  }
  </code></pre>
  
  <p>Writing the array to the text file doesn't work at all right now because it says the index is out of range, but when I commented it out and tried to only write the username and password, only the password would be in the file when I checked it. I was expecting the username, a comma, and then the password to be in it</p>