我在Swift项目中使用PayMill's iOS SDK。
我可以成功创建交易,他们已成功注册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)")
})
变量transaction
的类型为PMTransaction
。 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
或任何其他数据类型,以便将数据正确发送到我的服务器?
我尝试将dataObject
声明为NSObject
,然后再将其传递给Alamofire:
let dataObject:NSObject = transaction as NSObject
但它似乎对Alamofire没有任何影响。
为了测试,我尝试使用通用对象,并在通过Alamofire将其发布到我的服务器后成功存储在我的数据库中,因此问题在于外来数据类型PMTransaction
。
非常感谢任何建议,如果需要,我会非常乐意发布更多信息。
答案 0 :(得分: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
,请添加您的解决方案,我会接受您的回答。