5

Data.txt contains stuff like this : "Cat" "Dog" "Mouse"

I want to fill an array with strings from that file (dico[0] = "Cat", dico[1] = "Dog", aso).

I found this, How to call Objective-C's NSArray class method from within Swift? and Read and write data from text file, but when I use this code :

let bundle = NSBundle.mainBundle()
let path = bundle.pathForResource("data", ofType: "txt")
let dico = NSArray(contentsOfFile: path)

println("\(dico[0])")
println("\(dico.count)")

All I get is "nil" and "0".

I guess data in my file aren't written the way they should be and the code I use isn't right, but I can't figure why.

Moreover, when I use this code, it's OK :

    let bundle = NSBundle.mainBundle()
    let path = bundle.pathForResource("data", ofType: "txt")
    let dico = NSString(contentsOfFile: path)

    println("\(dico)")

The problem is that I don't want dico to be a string, I want it to be an array.

Community
  • 1
  • 1
Piercy
  • 473
  • 1
  • 6
  • 11
  • This post helped me understand error handling when getting a string array from a text file: http://codereview.stackexchange.com/a/100813/79551 – Suragch Aug 15 '15 at 02:34

3 Answers3

4

This is not how arrayWithContentsOfFile works.

It expects as its argument the path to a file containing a string representation of an array produced by the writeToFile:atomically: method.

For your purposes, you can use the second approach, complemented by calling componentsSeparatedByString() on your string.

let bundle = NSBundle.mainBundle()
let path = bundle.pathForResource("data", ofType: "txt")
let dico = NSString(contentsOfFile: path).componentsSeparatedByString("\n")
Cezar
  • 55,636
  • 19
  • 86
  • 87
  • @Piercy Glad to have helped. Also, please remember to accept/upvote this answer if you believed it solved your problem. You should also do the same for any answers you received in the past that solved your problem on previous questions. This rewards the answerer, gives you a little reputation bump and helps other members in the community identify a good answer. – Cezar Jun 20 '14 at 13:17
  • 1
    Voting requires 15 reputation, and I'm brand new here. ;) Though, be sure I'll vote up when I'll be able to. – Piercy Jun 20 '14 at 13:54
  • NSString(contentsOfFile: path) no longer works in swift – shim Oct 27 '14 at 00:19
1

NSArray's arrayWithContentsOfFile: initializer isn't meant to be used with regular text file. It's only meant to be used with files that were created using NSArray's writeToFile:atomically:

Assuming you want to split up the words in your text file into an array and assuming each word will be separated by a space, you can do something like this:

let bundle = NSBundle.mainBundle()
let path = bundle.pathForResource("data", ofType: "txt")
let dico = NSString(contentsOfFile: path)

let components = dico.componentsSeparatedByString(" ")

println("\(components)")
Lance
  • 8,872
  • 2
  • 36
  • 47
  • I'm new here, so I cannot vote. But I would if I could. You pointed out my mistake and gave me a solution. Thanks a lot. – Piercy Jun 20 '14 at 13:12
  • Good to hear, you should still be able to accept the answer :) That'll help out your reputation as well. – Lance Jun 20 '14 at 13:20
  • this gives me "init(contentsOfFile:) is unavailable" because it's deprecated – shim Oct 27 '14 at 00:18
  • you should use init(contentsOfFile:encoding:error:) instead. – Lance Oct 27 '14 at 17:21
1

The file you are trying to read must be created using writeToFile:atomically: method. This is what documentation says about it:

aPath - The path to a file containing a representation of an array produced by the writeToFile:atomically: method.

So you have to either create a file using the above-mentioned method or read it as a string and convert it to an array afterwards using for example componentsSeparatedByString: method.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143