2015-02-05 57 views
0

我在Swift項目中使用PayMill's iOS SDKSwift/PayMill:將PMTransaction轉換爲NSObject

我可以成功創建交易,它們已成功註冊到PayMill。創建交易之後,我有下面的代碼的交易數據傳送到使用Alamofire我的服務器:

//... PayMill SDK initialization here... 

PMManager.transactionWithMethod(paymentMethod, parameters: paymentParameters, consumable: true, 
    success: { (transaction) -> Void in 
     println("successfully created transaction: \(transaction)") 
     // pass the data to my Server using Alamofire 
     // Alamofire POST here... 
    }, failure: { (error) -> Void in 
     println("found error: \(error)") 
    }) 

的變量transactionPMTransaction型。 See here for more information about PMTransaction

Alamofire提供了以下錯誤:

2015-02-05 14:56:44.434 MyApp[4070:727012] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (PMTransaction)' 

我怎樣才能將它轉換爲,比如說,一個NSObject,或任何其它數據類型,這樣可以將數據正確發送到我的服務器?

我試着將它傳遞給Alamofire之前聲明dataObjectNSObject類型:

let dataObject:NSObject = transaction as NSObject 

但它似乎並沒有做出Alamofire任何區別。

爲了測試,我嘗試使用通用對象,並通過Alamofire將它發佈到我的服務器後成功存儲在我的數據庫中,所以問題在於外部數據類型PMTransaction

非常感謝您的任何建議,如果需要,我會很樂意發佈更多信息。

回答

0

我發現了一種更爲人工的方式來解決我的問題。請看下圖:

  var dataObject = [ 
       "id":"\(transaction.id)", 
       "description":"\(transaction.description)", 
       "amount":"\(transaction.amount)", 
       "origin_amount":"\(transaction.origin_amount)", 
       "currency":"\(transaction.currency)", 
       "status":"\(transaction.status)", 
       "livemode":"\(transaction.livemode)", 
       "created_at":"\(transaction.created_at)", 
       "updated_at":"\(transaction.updated_at)", 
       "response_code":"\(transaction.response_code)", 
      ] 

如果有人能想出更好的方式來轉換PMTransaction,請添加您的解決方案,我會接受你的答案。