本文介绍了如何从二维码SWIFT 4中检索所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在SWIFT 4中检索和显示有关二维码的所有信息时遇到问题。
我使用了带有文本扩展名的二维码生成器,我在其中添加了
{"number":"+33688888888","amount":"50"}
在我的函数中调用和显示信息
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)
{
if metadataObjects != nil && metadataObjects.count != 0
{
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if metadataObj.type == AVMetadataObject.ObjectType.qr
{
let info = HPPayMoneySuccessModel(titlePage: "Payment", imageInfo: "confirm_checked2", titleDescription: "Congratulations your transaction has been successfully completed", numberString: metadataObj.stringValue!, amountString: metadataObj.stringValue!, buttonTerminate: "OK")
let segueViewController = HPPayMoneySuccessViewController.init(nibName: "HPPayMoneySuccessViewController", bundle: nil, payMoneySuccessViewModel: info)
self.navigationController?.pushViewController(segueViewController, animated: true)
self.session.stopRunning()
}
}
}
他以{"number":"+33688888888","amount":"50"}
这样的字符串向我获取信息,但我只想要+33688888888
中的+33688888888
和amountString
中的50
请帮帮我。
推荐答案
您需要
guard let stringValue = metadataObj.stringValue else { return }
if let res = try? JSONSerialization.jsonObject(with:Data(stringValue.utf8), options: []) as? [String:String] ,let fin = res {
guard let number = fin["number"] , let amount = fin["amount"] else { return }
print(number)
print(amount)
}
或
if let res = try? JSONDecoder().decode(Root.self, from: Data(stringValue.utf8)) {
print(res.number)
print(res.amount)
}
struct Root : Decodable {
let number,amount:String
}
这篇关于如何从二维码SWIFT 4中检索所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!