title: Check local file size in Swift 4
body: <p>I have this code:</p>
  
  <pre><code>func fileSize(url: String?)-&gt; UInt64  {
      let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
  
  
      guard let fileUrl = URL(string:  url!) else {
          print("Problem in converting string to URL @@@@@@@@@@@ \(url)")
          return 0
      }
  
  
  
      let directory = paths[0] + "/" + (fileUrl.path)
      let fileManager = FileManager.default
      do {
          let attributes = try fileManager.attributesOfItem(atPath: (directory))
          var fileSize = attributes[FileAttributeKey.size] as! UInt64
          let dict = attributes as NSDictionary
          fileSize = dict.fileSize()
          return fileSize
      }
      catch let error as NSError {
          print("Something went wrong: \(error)")
          return 0
      }
  }
  </code></pre>
  
  <p>This function should delete files from the Docs directory on the mnemonic device.
  Unfortunately, it does not work and I get an error message every time:</p>
  
  <pre><code>- Problem in converting string to URL @@@@@@@@@@@@@@ Optional ("en / GET_LEAFLETS_PDF / Leaflet new_OK-prev.pdf")
  - Problem in converting string to URL @@@@@@@@@@@@@ Optional ("en / GET_TIPS_SLIDES / Golden video of frying ___ 006.jpg")
  - Problem in converting string to URL @@@@@@@@@@@@@ Optional ("en / GET_TIPS_SLIDES / Spaces od love ___ 016.jpg")
  etc
  </code></pre>
  
  <p>What is the mistake? Does anyone know how to fix it?</p>
  
  <p>****UPDATE****</p>
  
  <p>This is working code:</p>
  
  <pre><code>func fileSize(url: String?)-&gt; UInt64  {
      let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
  
      let pathsFile = paths[0] + "/" + url!
      let fileUrl = URL(fileURLWithPath: pathsFile)
  
      let fileManager = FileManager.default
      do {
          let attributes = try fileManager.attributesOfItem(atPath: (fileUrl.path))
          var fileSize = attributes[FileAttributeKey.size] as! UInt64
          let dict = attributes as NSDictionary
          fileSize = dict.fileSize()
          return fileSize
      }
      catch let error as NSError {
          print("Something went wrong: \(error)")
          return 0
      }
  }
  </code></pre>