Advertisement
eranseg

Seven Boom

Apr 1st, 2020
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.50 KB | None | 0 0
  1. import Cocoa
  2.  
  3. class FileWriter {
  4.     // Variables
  5.     // For creating string filename location
  6.     var documentPath, fileName, fullPath : String!
  7.     // For getting the ios file manager
  8.     var fileManager = FileManager.default
  9.    
  10.     init(fileName : String) {
  11.         self.fileName = fileName
  12.         fileSettings()
  13.        
  14.     }
  15.    
  16.     func fileSettings() {
  17.         // For getting document path, we need to use our file manager...
  18.         self.documentPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0]
  19.         self.fullPath = "\(documentPath!)/\(fileName!)"
  20.     }
  21.    
  22.     func test() {
  23.         print(self.fullPath!)
  24.     }
  25.    
  26.     func writeToFile(_ txt: String) {
  27.         var myData = readFromFile()
  28.         do {
  29.             myData += txt
  30.             try myData.write(toFile: self.fullPath, atomically: true, encoding: String.Encoding.utf8)
  31.             print("Wrote to file")
  32.         }
  33.         catch {
  34.             print("Error writing to file")
  35.         }
  36.     }
  37.    
  38.     func readFromFile() -> String {
  39.         var temp = ""
  40.         if fileManager.fileExists(atPath: fullPath) {
  41.             do {
  42.                 try temp = String(contentsOfFile: fullPath)
  43.             } catch {
  44.                 print("Cannot read from file \(self.fileName ?? "")") /// In string we can add ?? "with default content"
  45.             }
  46.         } else {
  47.             print("No such file in the system")
  48.         }
  49.         return temp
  50.     }
  51.    
  52.     func deleteFile() {
  53.         do {
  54.             try fileManager.removeItem(atPath: fullPath)
  55.             print("File \(fileName!) deleted successfully")
  56.         } catch {
  57.             print("Failed to delete file \(fileName ?? "Bad file name")")
  58.         }
  59.     }
  60. }
  61.  
  62. class SevenBoom {
  63.    
  64.     let fName = "SevenBoom.txt"
  65.     let fileWriter = FileWriter(fileName: "SevenBoom.txt")
  66.     var res : String = ""
  67.    
  68.     func runProc() {
  69.         for num in 1...100 {
  70.             if num % 7 == 0 || num % 10 == 7 || num / 10 == 7 {
  71.                 res.append("Boom\n")
  72.             } else {
  73.                 res.append("\(num)\n")
  74.                
  75.             }
  76.         }
  77.     }
  78.     func createResultFile() {
  79.         runProc()
  80.         fileWriter.writeToFile(res)
  81.     }
  82.    
  83.     func readResultFile() {
  84.         print(fileWriter.readFromFile())
  85.     }
  86. }
  87.  
  88. let SBoom = SevenBoom()
  89. SBoom.readResultFile()
  90. SBoom.createResultFile()
  91. SBoom.readResultFile()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement