【发布时间】:2020-04-17 11:11:42
【问题描述】:
我的代码检查代码是否:“此数据”不为空,我如何检查代码本身是否存在。有些响应可能会给我一个几乎只有时间戳的空 JSON。所以var代码不会在那里。
或者有没有更好的方法来做到这一点?因为我的 JSON 是
“基于文本输入的变量”导致“代码”可能不存在,其中将包含“某些信息”或“”
脚本顶部:
struct Variables: Decodable {
var code: String
}
typealias DecoderX = [String: Variables]
先前的函数设置来自用户文本的输入,这些输入与数据库进行交叉检查,因此只有在设置了 UserDefault 输入时才会调用 GetInfo。
func GetInfo() {
let Input1 = UserDefaults.standard.string(forKey: “UserInput1”) ?? “”
let Input2 = UserDefaults.standard.string(forKey: “UserInput2”) ?? “”
let Input3 = UserDefaults.standard.string(forKey: “UserInput3”) ?? “”
print(“Input Check 1: \(Input1) \(Input2) \(Input3)”)
// URL INFO With API key hidden
let jsonTask = URLSession.shared.dataTask(with: requestURL) { data, response, error in
if response == response {
if let data = data, let body = String(data: data, encoding: .utf8) {
do {
let json = try? decoder.decode(DeocderX.self, from: data);
DispatchQueue.main.async {
print(“Input Check 2: \(json![Input1]!.code) \(json![Input2]!.code) \(json![Input3]!.code)”)
if json?[Input1]?.code != nil {
print("Good Information 1")
} else {
print("Found Nothing 1")
}
if json?[Input2]?.code != nil {
print("Good Information 2")
} else {
print("Found Nothing 2")
}
if json?[Input3]?.code != nil {
print("Good Information 3")
} else {
print("Found Nothing 3")
}
}
// rest of code not applicable
【问题讨论】: