如何从二维码SWIFT 4中检索所有数据

How to retrieve all data from QR Code Swift 4(如何从二维码SWIFT 4中检索所有数据)
本文介绍了如何从二维码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中的+33688888888amountString中的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中检索所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)