23

Similar to how you get your apps version kCFBundleVersionKey how does one get the version of the package you are working in?

2
  • Did you ever find a solution to this? Commented Oct 4, 2023 at 5:45
  • @Kudit not really best approach is still the suggested answer by Luca D'Alberti below Commented Oct 4, 2023 at 11:36

3 Answers 3

10

I don't think it's possible at the moment to get the value easily. You can either read the Package.resolved file or you can set up a public constant where you would define the version of your library.

public let version = "0.0.1"

import MyLibrary

print(MyLibrary.version)

There isn't a standard way how to do this, every package could have a different constant (or not have it at all)

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

2 Comments

that is helpful, how would i read the Package.resolved file from swift ?
You would have to read the file (using FileManager or directly with Data) and then convert it to something you can read, for example using Decodable. Otherwise you could check whether the Swift Package Manager library provides an API to inspect the libraries.
4

You can check package version rules in project package dependencies tab. Check screenshot below. Propably it would be sufficient for most use cases. enter image description here

3 Comments

I think the author wants to access the versions dynamically, so this doesn't help during runtime
How do you open this window?
@Zorayr in the left pane click on Project name and then select project -> build dependencies
1

Edit: The context has changed, the question was to get the version of MyPackage I'm working on via code. There isn't any documented or recommended method as of now. Though this can be achieved by a hack through reading the Package.resolved file as mentioned by Malcolm.

I'm not deleting this answer for any future users who are looking for answer on how to get the exact latest version of a Package.

You can give the same major version number and give Update to latest package version option on Xcode. eg: If current version is 2.4.5 you just need to provide 2.0.0 and update. There is also an option to provide the exact version of swift package you need.

This is how

7 Comments

this is to install the package, im looking for the version from within the source class of the package
"Source class of the package", what exactly does that mean?
I am writing the package so the entry point into the package where you traditionally call Package.init()
Inside a package you get a folder called Sources
So you are asking how to know which Source of the package you have in your local system?
|

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.