0

I'm trying to read in a file from disk and parse its data into a nice format. However, the function is not returning anything. It returns an empty array. Why is this?

Note: I've been tinkering around with this and I've managed to simplify the previous 2 functions to just this one.

The function:

func openFile(_ fileName:String, _ fileType:String) -> [(Double, Double)] {

        let file = fileName + fileType //this is the file. we will write to and read from it

        var text2: String = ""

        if let dir = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first {

            let path = dir.appendingPathComponent(file)

            //reading
            do {
                text2 = try String(contentsOf: path, encoding: String.Encoding.utf8)
            }
            catch {/* error handling here */}


        }

        var pairs = [(Double,Double)]()
        var words: [String] = []

        for line in (text2.components(separatedBy: "\n").dropFirst()){
            if line != "" {
                words = line.components(separatedBy: "\t")
                pairs.append((Double(words[0])!,Double(words[1])!))

            }
        }

        return pairs
    }
loltospoon
  • 239
  • 1
  • 9
  • @dfd do I need the full path?? – loltospoon Mar 18 '17 at 02:03
  • @dfd didn't work. I passed `/home/user/Downloads/filename` and `url` was still `nil` – loltospoon Mar 18 '17 at 02:22
  • @dfd ok thank you!!! – loltospoon Mar 18 '17 at 02:33
  • Here's a question with an answer that may give you a good start. It searches a few directories, builds a URL and path, and then uses FileManager to see if the file exists. http://stackoverflow.com/questions/24181699/how-to-check-if-a-file-exists-in-the-documents-directory-in-swift?rq=1 –  Mar 18 '17 at 03:16
  • @loltospoon Bundle.main.url only returns urls for files that were bundled into your app. You can't get a url for files located at the downloads directory using it. – Leo Dabus Mar 18 '17 at 03:40
  • @LeoDabus well...that's a pretty big problem then haha woops – loltospoon Mar 18 '17 at 03:40
  • `let url = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!.appendingPathComponent(filename_val).appendingPathExtension(fileExtension_val)` – Leo Dabus Mar 18 '17 at 03:42
  • Why don't you just hide the extension using the method I suggested at your last question ? – Leo Dabus Mar 18 '17 at 03:44
  • @LeoDabus What will hiding the extension do? – loltospoon Mar 18 '17 at 03:49
  • If you get the file info in finder it has a hide extension check mark. my extension allows you to select or deselect it so if you look at the file in your finder it won't show the extension, the same applies to the open/save panel – Leo Dabus Mar 18 '17 at 03:51
  • @LeoDabus Oh I just needed to move on to some other parts that need fixing. If it so happens that your method is efficient in this project I'll implement it. – loltospoon Mar 18 '17 at 03:56
  • @LeoDabus for your `let url = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!.appendingPathComponent(filename_val)‌​.appendingPathExtens‌​ion(fileExtension_va‌​l)` code, it throws the error: `Consecutive declarations on a line must be separated by ';'` – loltospoon Mar 18 '17 at 04:16
  • `let url = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!.appendingPathComponent(filename_val).appendingPathExtension(fileExtensio‌​n_va‌​l)` it is correct probably an invisible character there – Leo Dabus Mar 18 '17 at 04:20
  • @LeoDabus I cannot find this invisible character. I copied your code directly. It seems to be pointing to the `.` after `(filename_val)` – loltospoon Mar 18 '17 at 05:00
  • This is for Swift 3 btw. Maybe your code is for a previous version of Swift? – loltospoon Mar 18 '17 at 05:01
  • Try retyping it again if the problem persists letting Xcode autocomplete it for you. I am using Swift 3.0.2 • Xcode 8.2.1. `let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(filename).appendingPathExtension(fileExtensio‌​n)` – Leo Dabus Mar 18 '17 at 16:47
  • it autofilled `.appendingPathComponent` – loltospoon Mar 18 '17 at 17:40

0 Answers0