我试图访问Main NSBundle以检索版本和构建信息。事实是,我想在swift中尝试它,我知道如何使用Build.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
在Objective-C中检索它。然而,我不知道从哪里开始使用swift,我试图用新语法编写它,但没有用。
答案 0 :(得分:133)
Swift语法有什么问题?这似乎有效:
if let text = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
print(text)
}
答案 1 :(得分:94)
Swift 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)"
}
见here。
答案 2 :(得分:17)
对于Xcode 6的最终版本,请使用
NSBundle.mainBundle().infoDictionary?["CFBundleVersion"] as? String
“?” infoDictionary之后的字符在这里很重要
答案 3 :(得分:10)
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))."
}
}
答案 4 :(得分:8)
这是获取Build和版本的简单方法。
适用于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)
}
目标C
NSString *build = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
NSString * currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
如果有任何问题,请告诉我。这对我有用。
答案 5 :(得分:8)
Swift 5.0
我为Swift 5创建了一个包装器,以在我所有应用程序中的某个位置获取一些与应用程序相关的字符串,称为 AppInfo 。
struct AppInfo {
var appName : String {
return readFromInfoPlist(withKey: "CFBundleName") ?? "(unknown app name)"
}
var version : String {
return readFromInfoPlist(withKey: "CFBundleShortVersionString") ?? "(unknown app version)"
}
var build : String {
return readFromInfoPlist(withKey: "CFBundleVersion") ?? "(unknown build number)"
}
var minimumOSVersion : String {
return readFromInfoPlist(withKey: "MinimumOSVersion") ?? "(unknown minimum OSVersion)"
}
var copyrightNotice : String {
return readFromInfoPlist(withKey: "NSHumanReadableCopyright") ?? "(unknown copyright notice)"
}
var bundleIdentifier : String {
return readFromInfoPlist(withKey: "CFBundleIdentifier") ?? "(unknown bundle identifier)"
}
var developer : String { return "my awesome name" }
// 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("The apps name = \(AppInfo.appname)")
答案 6 :(得分:7)
在swift中,我会将其作为UIApplication的扩展,如下所示:
extension 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)
答案 7 :(得分:7)
//返回应用的版本号
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
}
答案 8 :(得分:2)
此代码适用于Swift 3,Xcode 8:
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") ?? "0"
let build = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") ?? "0"
答案 9 :(得分:2)
对于Swift 3,将NSBundle替换为Bundle,mainBundle仅由main替换。
library(stringr)
word(sub('.*//', '', s), 1, sep = '/')
#[1] "F10"
#where
s <- '/home/ricardo/MultiClass/data//F10/1036.txt'
答案 10 :(得分:1)
[更新:Xcode 6.3.1]我尝试了以上所有内容,但这些都不适用于Xcode 6.3.1,但我发现这样做:
SQL> select * from OPS$E64ADM.SAPUSER
答案 11 :(得分:1)
另一种选择是在AppDelegate中定义变量:
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))"
}
可以作为AppDelegate中的变量引用
答案 12 :(得分:0)
斯威夫特3:
let textVersion
= Bundle.main.infoDictionary?["CFBundleVersion"] as? String
答案 13 :(得分:0)
SWIFT 3版
if let infoPath = Bundle.main.path(forResource: "Info.plist", ofType: nil),
let infoAttr = try? FileManager.default.attributesOfItem(atPath: infoPath),
let infoDate = infoAttr[.creationDate] as? Date
{
return infoDate
}
return Date()
答案 14 :(得分:0)
要获得框架的结果,您可以使用
let version = Bundle(for: type(of: self)).infoDictionary?["CFBundleShortVersionString"] as? String
或在测试中
let version = Bundle(for: <SomeClass>.self).infoDictionary?["CFBundleShortVersionString"] as? String