无法调用'执行'使用类型'(_)'的参数列表

时间:2017-05-15 14:13:45

标签: swift xcode square-connect

为什么这个Square示例直接用于自述文件?

    let callbackURL = URL(string: "OdinMobile://")!
    do {
        let amount = try SCCMoney(amountCents: money, currencyCode: "USD")

        let request : SCCAPIRequest =
            try SCCAPIRequest(
                callbackURL: callbackURL,
                amount: amount,
                userInfoString: userInfoString,
                merchantID: nil,
                notes: notes,
                customerID: nil,
                supportedTenderTypes: supportedTenderTypes,
                clearsDefaultFees: clearsDefaultFees,
                returnAutomaticallyAfterPayment: true
            )

    } catch let error as NSError {
        print(error.localizedDescription)
    }

    do {
        try SCCAPIConnection.perform(request)
    } catch let error as NSError {
        print(error.localizedDescription)
    }

我收到Cannot invoke 'perform' with an argument list of type '(_)',其中包含Overloads for 'perform' exist with these partially matching parameter lists: (SCCAPIRequest), (Selector!)的其他消息。我希望request成为SCCAPIRequest,为什么它不是一个人阅读?是因为它在do块中吗?

1 个答案:

答案 0 :(得分:2)

do关键字在其大括号内创建一个范围,如iffor循环,这意味着您创建的请求位于第一个范围内,而在第二个范围内不可用。因为在这两种情况下你都会使用相同的错误做同样的事情,所以你可以在同一范围内移动perform调用。

let callbackURL = URL(string: "OdinMobile://")!
do {
    let amount = try SCCMoney(amountCents: money, currencyCode: "USD")

    let request : SCCAPIRequest =
        try SCCAPIRequest(
            callbackURL: callbackURL,
            amount: amount,
            userInfoString: userInfoString,
            merchantID: nil,
            notes: notes,
            customerID: nil,
            supportedTenderTypes: supportedTenderTypes,
            clearsDefaultFees: clearsDefaultFees,
            returnAutomaticallyAfterPayment: true
        )
    try SCCAPIConnection.perform(request)
} catch let error as NSError {
    print(error.localizedDescription)
}