Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // MyFiles
- //
- // Created by Hackeru_Student on 5/24/18.
- // Copyright © 2018 Hackeru_Student. All rights reserved.
- //
- import UIKit
- class ViewController: UIViewController {
- //outlets
- @IBOutlet weak var txt: UITextField!
- @IBOutlet weak var data: UITextView!
- //variables
- //for creating a string for file name location
- var documentPath, fileName, fullPath: String!
- //for getting the ios defualt file manager
- var fileManager = FileManager.default
- override func viewDidLoad() {
- super.viewDidLoad()
- fileSettings()
- }
- //actions
- @IBAction func btnWrite(_ sender: Any) {
- if txt.text!.isEmpty {return}
- //writeToFile(txt: readFromFile()+txt.text!+"\r")
- writeToFile(readFromFile()+txt.text!+"\r")
- data.text! = readFromFile()
- txt.text=""
- }
- @IBAction func btnRead(_ sender: Any) {
- data.text! = readFromFile()
- }
- @IBAction func btnDelete(_ sender: Any) {
- do
- {
- try fileManager.removeItem(atPath: fullPath)
- data.text! = ""
- }
- catch
- {
- print ("can not delete file")
- }
- }
- //methods
- fileprivate func fileSettings()
- {
- fileName = "husam.txt"
- documentPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
- fullPath=documentPath+"/"+fileName
- print (fullPath!)
- data.text!=""
- }
- fileprivate func writeToFile (_ txt:String)
- {
- do
- {
- try txt.write(toFile: fullPath, atomically: true, encoding: String.Encoding.utf8)
- }
- catch
- {
- print ("can not write to file!!!!")
- }
- }
- fileprivate func readFromFile()->String
- {
- var temp=""
- if fileManager.fileExists(atPath: fullPath)
- {
- do{
- try temp = String(contentsOfFile: fullPath)
- }
- catch{
- print ("can not open file for read")
- }
- }
- return temp
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement