UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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:

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

4 directories, 7 files

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.