Getting Version and Build Info with Swift
What was wrong with the Swift syntax? This seems to work:
if let text = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
print(text)
}
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.
For the final release of Xcode 6 use
NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String
The "?" character after infoDictionary is important here