Text File Workshop: Update Text File

The text file operation, update file pretty much update an existing text file in the sandbox’s Documents folder. Further more, the app user is usually the one who initiate this operation by tapping a button on a view.

Code presented on this page assume you are using Xcode 7.0 and Swift version 2.0. So if you are using a older or newer version of Swift, it may produce errors. Fix them by using Xcode’s Fix-it tool. Also, I assume you aren’t a newbie to the Swift Programming Language and you know your way around Xcode Integrated Development Editor.

In today’s workshop, you will implement the update file operation in the NiftyTextFile project. It consist of the five tasks shown in the User Interaction Diagram.

User Action App Response
The user tap the “Update File” button
  • Dump the selected text file’s data in a temporary variable called, oldNote
  • Dump the oldNote in the textView
  • Dump the selected text file’s data in the textView
  • Write (save) the modified textView’s data back in the selected text file
  • Display a message in an alert view

By the way, last week, you setup a view and class file (UpdateFileViewController) that’ll implement the update a text file operation. Further more, you entered code in the class file to perform these tasks:

  • Pass the selected file’s data to the UpdateFileViewController class
  • Pass the selected file’s name to the UpdateFileViewController class
  • Print selected text file name and its data in the console

Now, here’s the first piece of code to implement the update a text file operation, in the UpdateFileViewController class.

override func viewDidLoad() {
  super.viewDidLoad()

  NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardDidShow:", name:UIKeyboardDidShowNotification, object: nil)

  oldNote = fileContent
  textView.text = oldNote
}

Here’s the second piece of code to add in the file. Put it in the updateButtonPressed() function.

 // Dismiss the keyboard
textView.resignFirstResponder()

// Validate the textView and display a message in an alert view
if textView.text.isEmpty {
  showAlertView("Your Note", msg: "It is required")
  textView.text = oldNote
} else {
  File.writeFile(selectedFileName, data: textView.text)
  showAlertView("Your Note", msg: "It was updated successfully in the file, \(selectedFileName)")
}

Test the Update File Code

That’s all the code you have to put in the UpdateFileViewController.swift file to implement the text file operation, update file. Run the app on the Simulator, select a text file, modify the textView, and click the Update File button. You’ll see this output on the Simulator’s screen:

niftytextfile-part5-1

By the way, feel free to comment out or remove these statements from the FileListViewController.swift file’s prepareForSegue() function.

next-iconThat’s all for today. Next week, you will implement the text file operation, delete a text file. Until then, comments are welcomed. 🙂

  • test

    Thank you so much for your teaching!