This should be easy but its got me stuck.
In my app, I have a global constant defined as
public let apiKey = "hjbcsddsbsdjhksdbvbsdbvsdbvs"
I want to use that apiKey in a library that I have added to my project as a SwiftPackage. I need it in the library to initiate a service.
Library.configure(withAPIKey: apiKey)
but I get the error
Cannot find apiKey in scope
I have tried wrapping the apiKey into a struct like so:
public struct globalConstants {
static let apiKey = "hjbcsddsbsdjhksdbvbsdbvsdbvs"
}
and using it as such:
Library.configure(withAPIKey: globalConstants.apiKey)
and I get a similar error message.
What am I missing?
You could do something like this by having a
Constants.swiftfile:Then, assuming the file you're calling the constant from is in the same project and target as this
Constants.swiftfile, you can call it as follows:If this doesn't work, check if this new file is actually included in the project and a member of your target. That might be the issue in your case.
As a side note, be careful when adding API keys, secrets, and other sensitive information to your application's codebase. You might want to add it to your
.gitignorefile, for example.