使用 Swift 在字符串中插入应用程序版本?
Insert app version inside string using Swift?
我在 Swift 中有一些代码,我想插入一个 属性 的应用程序(版本)。这可能吗?
var parameters: [String : AnyObject] = [
"offerSef" : url,
"userGuid" : user.userGuid,
"bottlesQty" : quantityIndex + 1,
"appId" : "iOS" + /* WANT TO INSERT APP VERSION HERE */,
"creditCardId" : payment.paymentID,
"billingAddressId" : payment.billingAddressId,
]
注意:这不是 iOS app, programmatically get build version 的副本,因为我的问题是关于如何使用 Swift(而不是 Objective-C)[=13= 以正确的方式做到这一点]
这是我从此处的另一个问题拼凑而成的答案,但我也想知道是否有人有意见或知道 better/cleaner 做这件事的方法。
//First get the nsObject by defining as an optional anyObject
if let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary["CFBundleShortVersionString"] {
//Then just cast the object as a String, but be careful, you may want to double check for nil
if let version = nsObject as String {
appId = appId + "-" + version
}
}
var parameters: [String : AnyObject] = [
"offerSef" : url,
"userGuid" : user.userGuid,
"bottlesQty" : quantityIndex + 1,
"appId" : appId,
"isMobile" : true,
"creditCardId" : payment.paymentID,
"billingAddressId" : payment.billingAddressId,
]
我在 Swift 中有一些代码,我想插入一个 属性 的应用程序(版本)。这可能吗?
var parameters: [String : AnyObject] = [
"offerSef" : url,
"userGuid" : user.userGuid,
"bottlesQty" : quantityIndex + 1,
"appId" : "iOS" + /* WANT TO INSERT APP VERSION HERE */,
"creditCardId" : payment.paymentID,
"billingAddressId" : payment.billingAddressId,
]
注意:这不是 iOS app, programmatically get build version 的副本,因为我的问题是关于如何使用 Swift(而不是 Objective-C)[=13= 以正确的方式做到这一点]
这是我从此处的另一个问题拼凑而成的答案,但我也想知道是否有人有意见或知道 better/cleaner 做这件事的方法。
//First get the nsObject by defining as an optional anyObject
if let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary["CFBundleShortVersionString"] {
//Then just cast the object as a String, but be careful, you may want to double check for nil
if let version = nsObject as String {
appId = appId + "-" + version
}
}
var parameters: [String : AnyObject] = [
"offerSef" : url,
"userGuid" : user.userGuid,
"bottlesQty" : quantityIndex + 1,
"appId" : appId,
"isMobile" : true,
"creditCardId" : payment.paymentID,
"billingAddressId" : payment.billingAddressId,
]