Ios 如何从二维码Swift 4中检索所有数据
我在检索和显示Swift 4中有关二维码的所有信息时遇到问题 我使用了一个带有文本扩展名的二维码生成器,其中添加了Ios 如何从二维码Swift 4中检索所有数据,ios,swift,qr-code,Ios,Swift,Qr Code,我在检索和显示Swift 4中有关二维码的所有信息时遇到问题 我使用了一个带有文本扩展名的二维码生成器,其中添加了 {“编号”:“+336888888”,“金额”:“50”} 在我的函数中调用和显示信息 func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { i
{“编号”:“+336888888”,“金额”:“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”:“+336888888”,“amount”:“50”}
,但我只想+336888888
在numberString
中,以及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)
}
或
这就是JSON,学习如何解析JSON。您可以使用
JSONSerialization
或Codable,如果您使用Swift 4+。谢谢,它起作用了:)
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
}