为什么这个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
块中吗?
答案 0 :(得分:2)
do
关键字在其大括号内创建一个范围,如if
或for
循环,这意味着您创建的请求位于第一个范围内,而在第二个范围内不可用。因为在这两种情况下你都会使用相同的错误做同样的事情,所以你可以在同一范围内移动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)
}