将字串转换成字典?

时间:2018-06-28 15:35:34

标签: swift dictionary qr-code

我有一个QR码扫描仪,它以字符串形式读取QR码。没有选择将其检测为字典的选项。因此,唯一的解决方案是将其转换为字典(我认为)。请记住,我正在使用swift并使用了来自苹果的AVFoundation。

此QR码将以字符串形式输出[“ test”:“ test123”]。如何将其转换为字典?

1 个答案:

答案 0 :(得分:2)

这就是我想出的。输出不是字典。

let test = "[\"test\": \"test123\"]"
    let data = test.data(using: .utf8)!
    do{
        let output = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String:String]
        print ("\(String(describing: output))")
    }
    catch {
        print (error)
    }
相关问题