2017-05-15 95 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請一定要接受這個答案,當你有機會...感謝隊友! –

相關問題