[英]Swift - Type '(String, JSON)' cannot conform to 'StringProtocol'; only struct/enum/class types can conform to protocols
I'm using SwiftyJSON .我正在使用SwiftyJSON 。 Ultimately I want to see if the "date"
value equals the value of selectedDate
and if so, print the "event"
value.最终我想看看"date"
值是否等于selectedDate
的值,如果是,则打印"event"
值。
But I haven't even got that far yet.但我什至还没有走到那一步。 I'm getting an error on my if
statement.我的if
语句出现错误。
The error I'm receiving says Type '(String, JSON)' cannot conform to 'StringProtocol'; only struct/enum/class types can conform to protocols
我收到的错误说Type '(String, JSON)' cannot conform to 'StringProtocol'; only struct/enum/class types can conform to protocols
Type '(String, JSON)' cannot conform to 'StringProtocol'; only struct/enum/class types can conform to protocols
. Type '(String, JSON)' cannot conform to 'StringProtocol'; only struct/enum/class types can conform to protocols
。
data.json数据.json
[
{
"date": "01.01",
"event": "Mom birthday",
},
]
ViewController.swift 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")
}
}
}
override func viewDidLoad() { super.viewDidLoad()覆盖 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()
}
You can modify related line like:您可以修改相关行,例如:
data.forEach { (key, data) in
if key == "date", data.stringValue == selectedDate {
print("found it!")
}
}
Or you can access directly like;或者你可以直接访问like;
data["date"].stringValue == selectedDate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.