转换器swift2到swift3不能正确返回?

时间:2017-01-24 09:21:51

标签: ios swift2 swift3

我想将我的代码源从swift 2.3转换为swift 3 但我不能,你可以帮助我吗?

enter image description here

for transaction:AnyObject in transactions {
            let trans = transaction as! SKPaymentTransaction
            print(trans.error)

            switch trans.transactionState {

            case .purchased:
                let prodID = buyingProduct
                switch prodID {
                case ?adsid:
                    print("remove ads")
                    defaults.set(true, forKey: "adsremoved")
                    didbuyad = true
                case ?levelsid:
                    print("add levels to account")
                    self.unlockAllLevels()
                default:
                    print("IAP not setup")
                }
                SKPaymentQueue.default().finishTransaction(transaction as!
SKPaymentTransaction)
                queue.finishTransaction(trans)
                break;
            case .failed:
                print("buy error")
                SKPaymentQueue.default().finishTransaction(transaction as!
SKPaymentTransaction)
                queue.finishTransaction(trans)
                break;
            default:
                print("default")
                break;

            }
        }
    }

不会返回正确的值

1 个答案:

答案 0 :(得分:0)

只需删除"?"来自案例。

for transaction:AnyObject in transactions {
        let trans = transaction as! SKPaymentTransaction
        print(trans.error)

        switch trans.transactionState {

        case .purchased:
            let prodID = buyingProduct
            switch prodID {
            case adsid:
                print("remove ads")
                defaults.set(true, forKey: "adsremoved")
                didbuyad = true
            case levelsid:
                print("add levels to account")
                self.unlockAllLevels()
            default:
                print("IAP not setup")
            }
            SKPaymentQueue.default().finishTransaction(transaction as!
                SKPaymentTransaction)
            queue.finishTransaction(trans)
            break;
        case .failed:
            print("buy error")
            SKPaymentQueue.default().finishTransaction(transaction as!
                SKPaymentTransaction)
            queue.finishTransaction(trans)
            break;
        default:
            print("default")
            break;

        }
    }