Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //files and ios sand box.....
- import UIKit //you must import UIKit for getting all the extra commands
- class FileWritter{
- //variebles
- //for createing string filename location
- var documentPath, fileName, fullPath:String!
- //for getting the ios file manager
- var fileManager = FileManager.default
- init() {
- fileSettings()
- }
- func fileSettings(){
- //name of file
- self.fileName = "zeev.txt"
- //for getting document path, we need to use our file manager....
- self.documentPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
- self.fullPath = "\(documentPath!)/\(fileName!)"
- }
- func test(){
- print (self.fullPath!)
- }
- func writeToFile(_ txt:String){
- var myData = readFromFile()
- myData += txt
- do{
- try myData.write(toFile: self.fullPath, atomically: true, encoding: String.Encoding.utf8)
- print ("wrote to file")
- } catch {
- print ("Error writting to file...")
- }
- }
- func readFromFile()->String{
- var temp = ""
- if fileManager.fileExists(atPath: fullPath){
- //read from file
- do{
- try temp = String(contentsOfFile: self.fullPath)
- } catch {
- //in string we can add ?? "with defualt value"
- print ("we can not read from file \(self.fileName ?? "filename currptted")")
- }
- } else {
- print ("there is no file in the system")
- }
- return temp
- }
- func deleteFile(){
- do{
- print ("file was deleted")
- try fileManager.removeItem(atPath: fullPath)
- } catch {
- print ("can not delete the file...")
- }
- }
- }
- var test = FileWritter()
- test.deleteFile()
- print (test.readFromFile())
- test.writeToFile("let's do what we did last night \n")
- print (test.readFromFile())
- test.writeToFile("we will rule the world !!")
- print (test.readFromFile())
- test.deleteFile()
- test.writeToFile("zeev")
- print (test.readFromFile())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement