2017-05-15 43 views
0

为什么这个Square示例直接针对他们的README不起作用?无法用类型为'(_)'的参数列表调用'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 
      ) 

    } 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区块吗?

回答

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) 
} 
+0

我想,我应该自己试过。 –

+0

@NilsGuillermin请一定要接受这个答案,当你有机会...感谢队友! –

相关问题