ardakaraca ardakaraca - 4 months ago 8
iOS Question

Does using a path from system, while calling a file, cause crashes?

I've a text file that stores my app's data for me.

If I use system path

(/Users/username/Desktop/ProjectFile/data.txt)
, can it cause errors when application compiled into .ipa file?

Answer

Using path form system like in the question causes crash when tested on a real iOS device.

Instead of using system path, using the code below will be more advantageous. It prevents crashes on real device.

        let path = NSBundle.mainBundle().pathForResource("file", ofType: "txt")
        let fileMgr = NSFileManager.defaultManager()
        var array: [String] = []

        if fileMgr.fileExistsAtPath(path!) {
            do {
                let text = try! String(contentsOfFile: path!, encoding: NSASCIIStringEncoding)
                array = text.componentsSeparatedByString("|#|")
            }
        }