NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Swift package manager - where do I put an input.txt file..? [EDIT: Solved in Swift 5.3]

Forums > Swift

[EDIT: Solved in Swift 5.3] Swift 5.3 introduces the possibility to add resourses to the packages..

example: .target( name: "challenges", dependencies: [], resources: [.copy("input.txt"), .copy("image.png")] )

However if anyone knows how to make it work for swift 5.2 pls advise!

[Original post]

I got a problem here.. I am probably missing something. I spent an hour trying dfferent things but this file is not seen by the system, no matter where I put it...

Any help is appreciated

I am trying to write a solution to an algorythm challenge using a Swift Package Executable.

I have an input.txt file containing a long string of number...

I tried to import it in main like this:

let my inputString = try! String(contentsOfFile: "input.txt")

I get the Fatal Error at runtime:

Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=260 "The file “input.txt” couldn’t be opened because there is no such file." UserInfo={NSFilePath=input.txt, NSUnderlyingError=0x10060af80 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

I tried to put the file in different places but it just does not work...

This is my package structure:

➜ tree
├── Package.swift
├── Sources
│   ├── challenges
│   │   └── main.swift
│   └── input.txt
└── Tests
    ├── LinuxMain.swift
    └── challengesTests
        ├── XCTestManifests.swift
        └── challengesTests.swift

4 directories, 7 files


Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.