8

I read through these SO links for the answer

1. Read a file/URL line-by-line in Swift

2. Read and write data from text file

Link 2 Provided me the solution but the problem is directory. It is by default Document directory of the current project.

So if i want to read a file from "/Users/Prem/Desktop/File.txt", what modification i should make to that code?

How to work with custom directory to read and write data in a text file?

let dirs : [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String]

if ((dirs) != nil) {
    var dir = dirs![0]; //documents directory
    //println(dir)

    //dir = "/Users/Prem/Desktop"  ----> If i give this path, its not creating the file

    //Current Value in dir
    //dir ----> "/Users/Prem/Library/Containers/com.apple.dt.playground.stub.OSX.FileIO-2159807F-CC21-4545-AC34-AD9F9898796B/Data/Documents"

    let path = dir.stringByAppendingPathComponent("File.txt");
    println(path)
    let text = "Rondom Text"

    //writing
    text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding, error: nil);

    //reading
    let text2 = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
}
Community
  • 1
  • 1
iamprem
  • 606
  • 2
  • 9
  • 23
  • Run that code, print out dir on line 4 (after `let dir = ...`) and it should tell you the format you need the dir string in. (you can and probably should do the same for path) – Millie Smith Feb 11 '15 at 20:10
  • Just assign `let path = /path/to/your/file.txt` ... – Martin R Feb 11 '15 at 20:11
  • @MartinR @MillieSmith : The 'dir' variable contains `/Users/Prem/Library/Containers/com.apple.dt.playground.stub.OSX.FileIO-2159807F-CC21-4545-AC34-AD9F9898796B/Data/Documents` But if i replace the path to : `Users/Prem/Desktop` or somewhere other than the above path, it is not writing the file. – iamprem Feb 11 '15 at 20:54
  • @MartinR Hey any good? I'm not able to figure it our yet. – iamprem Feb 12 '15 at 16:11
  • @robmayoff Thanks for giving me the answer. Now I tried with an xcode project and it worked well. :) I'm just adding your word as an answer to this question for easy reference. – iamprem Feb 13 '15 at 04:50
  • 1
    It is asinine to close questions as duplicate when they ask a different question than the duplicate. While, yes, the reason is the same, the questions are distinct. In this case, this question is better, covering the entire topic of file access in a playground, but came later. – Cameron Lowell Palmer May 01 '16 at 16:56
  • This question is not a duplicate, the other question don't talk at all about writing file from a Playground, and the answers heres is miss leading. There is a better answer to this question. – Vincent Bernier Sep 28 '16 at 12:27
  • @VincentBernier what do you mean by better answer? can we access any files in playground. If you know how to do that, please update the answer below. – iamprem Sep 28 '16 at 17:36
  • you can't access any file, but you can write a file to a specific location. – Vincent Bernier Sep 30 '16 at 14:28
  • you can only access file that are in the resource folder, but you can write to a specific location outside of the playground. I can't add an answer since this question was flag as duplicate. – Vincent Bernier Sep 30 '16 at 17:30

1 Answers1

9

After trying out for couple of days it is clear that,

We cannot access other than the project's self contained directories, if we try 'file handling' in playground. Because it is sandboxed

But from an xcode project, we can access any directory and perform read/write operations.

credits: @robMayoff

iamprem
  • 606
  • 2
  • 9
  • 23