0

I have a plaintext file that simply has a list of words. I’d like to bring this list in as an array but I haven’t had any luck. I can also bring it in as a string and convert it to an array, but I can’t get that to work either.

let location = "/Users/user/Desktop/list.txt"
var content = NSString(contentsOfFile: location, encoding: NSUTF32StringEncoding, error: nil)
println(content)

My output is always “nil". Same thing if I try to do an array instead:

let content2 = NSArray(contentsOfFile: location)
println(content2)
2
  • Did you try another encoding? Perhaps NSUTF8StringEncoding instead of NSUTF32StringEncoding? – And why don't you use the error parameter to get information about the problem? Commented Mar 26, 2015 at 20:34
  • Are you running this code in playground or an actual Xcode project? Commented Mar 26, 2015 at 20:41

4 Answers 4

1

change:

var content = NSString(contentsOfFile: location, encoding: NSUTF32StringEncoding, error: nil)

to:

var content = NSString(contentsOfFile: location, encoding: NSUTF8StringEncoding, error: &err)

EDIT: removed nil from suggested fix per Martin R's request

Sign up to request clarification or add additional context in comments.

1 Comment

@MartinR I will fix that I just copied his code and changed what would make it work
1

i use this helper class for file working:

class File {
    var path=""

    init(path: String,name:String){
        self.path=path+"/"+name
    }

    init(path: String){
        self.path=path
    }

    func read()->String {
        return NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)!
    }

    func write(data: String) {
        data.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
    }

    func getLineArray()->[String] {
        return read().componentsSeparatedByString("\n")
    }
}

using:

let address = "/Users/sajadgarshasbi/Desktop/myTestFile/sample.txt"
if NSFileManager.defaultManager().fileExistsAtPath(address) {
    let f = File(path: address)
    println(f.read())
}else{
    println("File Not Found")
}

Comments

0

Does that file exist? What does

NSFileManager().fileExistsAtPath(location)

return?

Also, are you sure it's UTF-32 encoded? The most likely encoding is probably UTF-8.

Comments

0

If you are using playground it will always return nil because playground is sandboxed. You can place your file inside the documents directory but it is located somewhere else at your computer and probably looks like something like the String below.

"/var/folders/f9/yrxcqv_10m57prx9lwts4qy80000gq/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-iOS-NSDate-85BEA777-C527-4A79-9B4E-53427CF98B7D/Documents/".

You can open this folder using your finder Main Menu > Go > Go To Folder... (command-shift-G) and paste that directory path there.

You can locate your playground documents folder using the code below:

let documentDirectoryPath =  (NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as NSURL).path!

Just place your file there and then you can open it as a String:

let fileName = documentDirectoryPath.stringByAppendingPathComponent("test.txt")

var error:NSError?
if "This is a sample sentence.".writeToFile(fileName, atomically: true, encoding: NSUTF8StringEncoding, error: &error) {
    println("file successfully saved")  // "file successfully saved"
} else {
    if let error = error {
        println(error.description)
    }
}


if let myLoadedString = String(contentsOfFile: fileName, encoding: NSUTF8StringEncoding, error: &error) {
    println(myLoadedString)  // "This is a sample sentence."
} else {
    if let error = error {
        println(error.description)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.