Similar to how you get your apps version kCFBundleVersionKey how does one get the version of the package you are working in?
-
Did you ever find a solution to this?Kudit– Kudit2023-10-04 05:45:07 +00:00Commented Oct 4, 2023 at 5:45
-
@Kudit not really best approach is still the suggested answer by Luca D'Alberti belowMalcolmn Roberts– Malcolmn Roberts2023-10-04 11:36:18 +00:00Commented Oct 4, 2023 at 11:36
3 Answers
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)
2 Comments
Package.resolved file from swift ?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.You can check package version rules in project package dependencies tab. Check screenshot below. Propably it would be sufficient for most use cases.

3 Comments
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.
7 Comments
entry point into the package where you traditionally call Package.init()Sources