我想从我的应用程序中删除计划通知,为此我需要String数组来删除待处理的通知。我有这种类型的字符串,而不是从fbdb数据库返回String的数组,但是我想要数组
["01D94B0E-F1AB-421E-9EC3-4A78F0211ED8",
"76E16E51-CB59-4D3F-939E-4D492FFB22BE",
"97696EBD-252F-4A12-962E-995EF306B557",
"84EB98BB-14EB-4D19-83F6-798DCF75E3CD",
"B55621AE-B124-4767-8D6E-C728598E5279"]
我有此格式的数组,该怎么办?我知道这是初学者的问题,希望你们能帮忙
答案 0 :(得分:1)
let str = """["01D94B0E-F1AB-421E-9EC3-4A78F0211ED8", "76E16E51-CB59-4D3F-939E-4D492FFB22BE", "97696EBD-252F-4A12-962E-995EF306B557", "84EB98BB-14EB-4D19-83F6-798DCF75E3CD", "B55621AE-B124-4767-8D6E-C728598E5279"]"""
let ids = try? JSONSerialization.jsonObject(with: Data(str.utf8)) as? [String] ?? []
答案 1 :(得分:0)
您可以使用默认的JSONDecoder ...
import Foundation
var stringToData: Data? = "[\"01D94B0E-F1AB-421E-9EC3-4A78F0211ED8\", \"76E16E51-CB59-4D3F-939E-4D492FFB22BE\", \"97696EBD-252F-4A12-962E-995EF306B557\", \"84EB98BB-14EB-4D19-83F6-798DCF75E3CD\", \"B55621AE-B124-4767-8D6E-C728598E5279\"]".data(using: .utf8)
let decoded = try JSONDecoder().decode(Array<String>.self, from: stringToData!)
print(decoded)
输出:
[“ 01D94B0E-F1AB-421E-9EC3-4A78F0211ED8”,“ 76E16E51-CB59-4D3F-939E-4D492FFB22BE”,“ 97696EBD-252F-4A12-962E-995EF306B557”,“ 84EB98BB-14EB-4D19-83F6-798DCF75 ”,“ B55621AE-B124-4767-8D6E-C728598E5279”]
答案 2 :(得分:-2)
您可以尝试
let str = """
["1","2","3"]
"""
let res = str.dropFirst(1).dropLast(1)
let arr = res.components(separatedBy: ",").map { $0.replacingOccurrences(of: "\"", with: "") }