简体   繁体   English

Swift - 类型“(字符串,JSON)”不能符合“字符串协议”; 只有结构/枚举/类类型可以符合协议

[英]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.

相关问题 Swift 协议类型“XXX”的值不能符合“可识别”; 只有结构/枚举/类类型可以符合协议 - Swift Value of protocol type 'XXX' cannot conform to 'Identifiable'; only struct/enum/class types can conform to protocols 类型“MovieSearchContainer.Type”不能符合“Decodable”; 只有结构/枚举/类类型可以符合协议 - Type 'MovieSearchContainer.Type' cannot conform to 'Decodable'; only struct/enum/class types can conform to protocols Swift 错误:“类型 '()' 不能符合 'View';只有 struct/enum/class 类型可以符合协议”调用函数写入文本时 - Swift error: "Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols" when calling function to write text 类型 '()' 不能符合 'View'; 只有 struct/enum/class 类型可以符合使用 swift ui 调用调用函数的协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols calling calling functions with swift ui 类型 '() -> ()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '() -> ()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 协议类型“Any”的值不能符合“Equatable”; 只有结构/枚举/类类型可以符合协议 - Value of protocol type 'Any' cannot conform to 'Equatable'; only struct/enum/class types can conform to protocols navigationBarItems“类型[视图]不能符合'视图'; 只有结构/枚举/类类型可以符合协议” - navigationBarItems “Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols” 类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 协议类型'*'的值不能符合'*'; 只有结构/枚举/类类型可以符合协议 - Value of protocol type '*' cannot conform to '*'; only struct/enum/class types can conform to protocols 使用 if 语句时:类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - While using an if statement: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM