0

I have some code in Swift and I'd like to insert a property of the app (the version). Is this possible?

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

Note: This is not a duplicate of iOS app, programmatically get build version because my question is about how to do it the right way using Swift (as opposed to Objective-C)

Community
  • 1
  • 1
Ben H
  • 3,136
  • 3
  • 25
  • 34

1 Answers1

0

This is an answer I pieced together from another question on here but I am also wondering if anyone has comments or knows a better/cleaner way of doing it.

    //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,
    ]
Ben H
  • 3,136
  • 3
  • 25
  • 34