0

I want to parse a .csv file with Xcode. I have made the program with the CSVImporter (https://github.com/Flinesoft/CSVImporter). It was the first extern library I have ever used.

The program does work well on the iOS Simulator in Xcode. But when I want to download it on my real iPhone, it can't parse the .csv. I think it because of the path of the .csv. The path is different when I download the program on the iPhone but I don't know how to get the real path.

Here is my code (Example):

var Vokabeln = [[String]]()
var Vokabelzähler = 0

override func viewDidLoad() {
        super.viewDidLoad()

        let VokabelPath = "/Users/---CENSORED---/Documents/TestLöschen/TestLöschen/Veronika-Sprache.csv"
        let importer = CSVImporter<[String]>(path: VokabelPath, delimiter: ";")
        let importedRecords = importer.importRecords { $0 }
        for record in importedRecords {
            self.Vokabeln.insert(record, at: self.Vokabelzähler)
            self.Vokabelzähler += 1
        }
        print(Vokabeln)
    }

When I use this in the iOS Simulator it works well and prints the new array. But if I use it on the iPhone it will print only [], an empty array. And if I make a breakpoint at self.Vokabeln.insert(record, at: self.Vokabelzähler) it won't stop. So it doesn't read the code in the for loop which means the path must be wrong.

But how can I get the real path? Can you help me?

Onur A.
  • 3,007
  • 3
  • 22
  • 37
Blub
  • 121
  • 1
  • 1
  • 6
  • The device has no magic access to files on your computer. You have to 1) add the csv file as a resource in the Xcode project, and 2) use the (NS)Bundle APIs to locate the file on the device at runtime. – Martin R Mar 19 '17 at 09:55
  • @MartinR 1. The file is in the Xcode projrct (at least in the folder). 2. I don't know how to do this. Can you give me an tutorial? – Blub Mar 19 '17 at 10:44
  • @MartinR http://stackoverflow.com/questions/32665546/swift-how-to-add-file-to-a-project-and-reference-it I have added my file to the project as written as in the first answer, so it is inn the xcode project – Blub Mar 19 '17 at 10:50
  • See http://stackoverflow.com/a/25349246/1187415 for an example. – Martin R Mar 19 '17 at 10:52
  • @MartinR Thank you, it worked – Blub Mar 19 '17 at 15:27
  • @MartinR You could have made this as an asnwer, then you would become an hook. – Blub Mar 19 '17 at 15:27

0 Answers0