使用Swift在app中插入app版本?

时间:2017-02-27 19:09:34

标签: swift plist

我在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)

1 个答案:

答案 0 :(得分: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,
    ]