2017-02-27 38 views
0

我在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重複的,因爲我的問題是關於如何做到這一點用斯威夫特以正確的方式(而不是Objective-C的)

回答

0

這是一個答案,我從拼湊另一個問題在這裏,但我也想知道如果任何人有評論或知道一個更好/更乾淨的方式做到這一點。

//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, 
    ]