Hello All,
I'm currently writing a Discord Bot using the Sword API by Azoy (don't worry that doesn't really show up here). Previously, I had an issue with my code and it was suggested that I delete my .xcodeproj file and regenerate it via SPM using swift package generate-xcodeproj
. This method keeps all my other files intact and just makes a new .xcodeproj file.
Fortunately, my issue was fixed. Unfortunately, this process may have broken something else...
I have attached a snippet below of my now problematic code. It worked fine before the aforementioned process and I'm out to discover why it has ceased proper function.
Is anyone able to help me figure out why my code can't move past the guard let
when config.json is in my build directory next to the executable?
My code will only fulfill the else
statement in my guard let
though the directories printed in the console and the directories in Finder are identical.
Through some (minor) debugging I arrived here:
import Sword
import Foundation
struct Config: Codable {
let token: String
let cmdPrefix_ES: String
let cmdPrefix_JA: String
}
enum ConfigGetError: Error {
case cannotMakeURL
}
func getBotConfig() throws -> Config {
print(Bundle.main.executablePath!)
print(Bundle.main.bundlePath)
let contents = try? FileManager.default.contentsOfDirectory(atPath: Bundle.main.executablePath!)
print( contents! )
guard let configFileURL = Bundle.main.url(forResource: "config", withExtension: "json") else {
print("Can't make URL.")
throw ConfigGetError.cannotMakeURL
}
print(configFileURL)
let configFileData = try Data(contentsOf: configFileURL)
return try JSONDecoder().decode(Config.self, from: configFileData)
}
print( contents! )
returns nil
which shows up as an error in Xcode
So my question now becomes
why is contents
returning nil
?
Is there something more to Bundle
?
Thank you
- L33t
EDIT: We figured it out! Big surprise, I'm a dumb dumb
I put the new build directory inside of the old build directory when I should've just selected the old one