【发布时间】:2020-10-12 16:09:03
【问题描述】:
我正在使用 SwiftyJSON。最终我想看看"date"的值是否等于selectedDate的值,如果是,打印"event"的值。
但我还没走到那一步。我的if 声明出现错误。
我收到的错误是Type '(String, JSON)' cannot conform to 'StringProtocol'; only struct/enum/class types can conform to protocols。
数据.json
[
{
"date": "01.01",
"event": "Mom birthday",
},
]
ViewController.swift
var json:JSON = false
var selectedDate:String = "01.01"
func updateView() {
json.forEach { (key, data) in
if key == "date", data.stringValue == selectedDate {
print("found it!")
} else {
print("no matches")
}
}
}
覆盖 func viewDidLoad() { super.viewDidLoad()
// Get JSON data
if let path = Bundle.main.path(forResource: "Devotions", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
json = try JSON(data: data)
} catch let error {
print("parse error: \(error.localizedDescription)")
}
} else {
print("Invalid filename/path.")
}
updateView()
}
【问题讨论】:
标签: swift swifty-json