2

I have the following code which creates a string array of a text file named num.txt

import Foundation
import UIKit

func linesFromResource(fileName: String) throws -> [String] {

    guard let path = NSBundle.mainBundle().pathForResource("num", ofType: "txt") else {
        throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo: [ NSFilePathErrorKey : fileName ])
    }
    let content = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
    return content.componentsSeparatedByString("\n")
}

let lines = try linesFromResource("num.txt")
print(lines)

my file num.txt is located in the resource folder of the playground

Playground

And it works fine and does what is intended, but when I try to do exactly the same thing in a project it gives me this error message:

Project

For some reason in the project the file "num.txt" can't be called I've tried creating a Resources folder and putting the file there but it didn't work either, what am I doing wrong?

1
  • Have you tried debugging it? Commented Aug 14, 2016 at 3:40

3 Answers 3

2

Please check that num.txt file is added in the Copy Bundle Resources, To check that go here ProjectModule->Build Phases->Copy Bundle Resources. For getting more idea check image.

enter image description here

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

7 Comments

thanks, following your advice I put it there, I checked and it is there but I'm still getting the same error
Try to clean build your project and also remove the derive data.
I did all that, went to the derived data directory and deleted everything there, I also pressed cmd + shit + k to clean it but I'm still getting the same error for some strange reason the file isn't being read.
I don't understand what problem you are facing, because this will sure solved the issue.
I solved the issue, just to let you know, for some reason when I try to compile a command line application for OSX this doesn't work, I tried to do it as a single view application for iOS and then it worked. I just had to place the code in viewDidLoad and add an extra catch do { let lines = try linesFromResource("num.txt") print(lines) } catch { print("File not found") } Now I'm wondering how could this work in a command line application for OSX in Swift? thanks for the help!
|
1

I solved the issue, for some reason when I try to compile a command line application for OSX Copying as Bundle resource doesn't work, I tried to do it as a single view application for iOS and then it worked.

I was able to make it run as an OSX command line application as well by adding the file as Copy Files instead of Copy Bundle Resources and by selecting resources as destination, I had tried this before but not without first deleting the derived data and clean building as NDoc suggested so I guess both things together did the trick.

Copy Files

Comments

0

Change linesFromResource("num.txt") to linesFromResource("num"), the extra .txt is the problem.

1 Comment

Thanks, but I followed your advice and I'm still getting the same problem

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.