Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting version and build information with Swift

I am attempting to gain access to the main NSBundle to retrieve version and build information. Thing is, I want to try it in Swift, I know how to retrieve it in Objective-C with:

text = [NSBundle.mainBundle.infoDictionary objectForKey:@"CFBundleVersion"]; 

Yet I don't know where to start with Swift, I have attempted to write it in the new syntax with no avail.

like image 409
Ken-UbiDex Avatar asked Jul 01 '14 01:07

Ken-UbiDex


People also ask

What is version and build in Xcode?

These are the actual software version numbers, as opposed to the values used for marketing. If you go to Xcode menu > About Xcode, you'll see the Version and Build numbers. If you hit the More Info... button you'll see a bunch of different versions. Since the More Info...


2 Answers

What was wrong with the Swift syntax? This seems to work:

if let text = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {     print(text) } 
like image 90
Connor Avatar answered Sep 26 '22 01:09

Connor


Swift 3/4 Version

func version() -> String {     let dictionary = Bundle.main.infoDictionary!     let version = dictionary["CFBundleShortVersionString"] as! String     let build = dictionary["CFBundleVersion"] as! String     return "\(version) build \(build)" }  

Swift 2.x Version

func version() -> String {     let dictionary = NSBundle.mainBundle().infoDictionary!     let version = dictionary["CFBundleShortVersionString"] as String     let build = dictionary["CFBundleVersion"] as String     return "\(version) build \(build)" } 

as seen here.

like image 45
Dan Rosenstark Avatar answered Sep 26 '22 01:09

Dan Rosenstark