我正在尝试访问主要的NSBundle
以检索版本和构建信息。问题是,我想在Swift中尝试它,我知道如何在Objective-C中检索:
text = [NSBundle.mainBundle.infoDictionary objectForKey:@"CFBundleVersion"];
我不知道从哪里开始学习Swift,我尝试使用新的语法编写它,但没有成功。
我正在尝试访问主要的NSBundle
以检索版本和构建信息。问题是,我想在Swift中尝试它,我知道如何在Objective-C中检索:
text = [NSBundle.mainBundle.infoDictionary objectForKey:@"CFBundleVersion"];
我不知道从哪里开始学习Swift,我尝试使用新的语法编写它,但没有成功。
Swift语法有什么问题?这个似乎可以解决:
if let text = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
print(text)
}
let
更自然一些,但它看起来是正确的。 - Tommylet text = NSBundle.mainBundle().infoDictionary["CFBundleVersion"]! as String
- E. BarnesSwift 3/4 版本
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 版本
func version() -> String {
let dictionary = NSBundle.mainBundle().infoDictionary!
let version = dictionary["CFBundleShortVersionString"] as String
let build = dictionary["CFBundleVersion"] as String
return "\(version) build \(build)"
}
可以在这里看到。
关于 Swift 5.0
我创建了一个包装器,将所有我的应用程序中的一些与应用相关的字符串集中在一个地方,称为AppInfo。
/// Wrapper to get some app related strings at one place.
struct AppInfo {
/// Returns the official app name, defined in your project data.
var appName : String {
return readFromInfoPlist(withKey: "CFBundleName") ?? "(unknown app name)"
}
/// Return the official app display name, eventually defined in your 'infoplist'.
var displayName : String {
return readFromInfoPlist(withKey: "CFBundleDisplayName") ?? "(unknown app display name)"
}
/// Returns the official version, defined in your project data.
var version : String {
return readFromInfoPlist(withKey: "CFBundleShortVersionString") ?? "(unknown app version)"
}
/// Returns the official 'build', defined in your project data.
var build : String {
return readFromInfoPlist(withKey: "CFBundleVersion") ?? "(unknown build number)"
}
/// Returns the minimum OS version defined in your project data.
var minimumOSVersion : String {
return readFromInfoPlist(withKey: "MinimumOSVersion") ?? "(unknown minimum OSVersion)"
}
/// Returns the copyright notice eventually defined in your project data.
var copyrightNotice : String {
return readFromInfoPlist(withKey: "NSHumanReadableCopyright") ?? "(unknown copyright notice)"
}
/// Returns the official bundle identifier defined in your project data.
var bundleIdentifier : String {
return readFromInfoPlist(withKey: "CFBundleIdentifier") ?? "(unknown bundle identifier)"
}
var developer : String { return "my awesome name" }
// MARK: - Private stuff
// lets hold a reference to the Info.plist of the app as Dictionary
private let infoPlistDictionary = Bundle.main.infoDictionary
/// Retrieves and returns associated values (of Type String) from info.Plist of the app.
private func readFromInfoPlist(withKey key: String) -> String? {
return infoPlistDictionary?[key] as? String
}
}
使用:
print("Name of the app: \(AppInfo().appName)")
CFBundleDisplayName
。 - gondo请使用Xcode 6的最终版本进行发布。
NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String
这里的 infoDictionary 后面的 "?" 字符很重要。
这里有一种简单的方法来获取构建版本信息。
对于 Swift 4.X
if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
print(version)
}
if let build = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
print(build)
}
对于 Objective C
NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSString * currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
如果出现任何问题,请告诉我。这个对我有效。
获取 AppName
、AppVersion
和 BuildNumber
的简单方式...
if let dict = NSBundle.mainBundle().infoDictionary {
if let version = dict["CFBundleShortVersionString"] as? String,
let bundleVersion = dict["CFBundleVersion"] as? String,
let appName = dict["CFBundleName"] as? String {
return "You're using \(appName) v\(version) (Build \(bundleVersion))."
}
}
Bundle.main
的 infoDictionary
是空的;可能是因为我是在一个框架内进行操作,而不是在可执行文件或应用程序中?Bundle(for: MyClass.self)
包含了预期的值。 - Raphaelextension UIApplication {
func applicationVersion() -> String {
return NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
}
func applicationBuild() -> String {
return NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as! String
}
func versionBuild() -> String {
let version = self.applicationVersion()
let build = self.applicationBuild()
return "v\(version)(\(build))"
}
}
let version = UIApplication.sharedApplication.applicationVersion() // 1
let build = UIApplication.sharedApplication.applicationBuild() // 80
let both = UIApplication.sharedApplication.versionBuild() // 1(80)
//返回应用程序的版本号
public static var appVersion: String? {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}
//返回应用程序的构建版本号
public static var appBuild: String? {
return Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String
}
var applicationVersion:String {
return NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
}
var applicationBuild:String {
return NSBundle.mainBundle().objectForInfoDictionaryKey(kCFBundleVersionKey as String) as! String
}
var versionBuild:String {
let version = self.applicationVersion
let build = self.applicationBuild
return "version:\(version) build:(\(build))"
}
这段代码适用于Swift 3和Xcode 8:
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0"
let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0"