简体   繁体   English

SwiftUI-类型“服务”不符合协议“可解码”

[英]SwiftUI - Type 'Service' does not conform to protocol 'Decodable'

I'm playing around with SwiftUI on a little app I'm building but I'm running into an issue with Codable and I'm getting a few errors. 我正在构建的一个小应用程序上正在使用SwiftUI,但是我遇到了Codable的问题,并且遇到了一些错误。

I have a JSON file which looks like this 我有一个看起来像这样的JSON文件

[
    {
        "id": "",
        "name": "services",
        "items": [
            {
                "id": 0
                "businessName": "Some Name",
                "businessTelephone": "01234567890",
                "businessEmail": "email@gmail.com",
                "businessWebsite": "website.com",
                "businessLocation": { "latitude": "54.137256", "longitude": "-1.524727" },
                "travelLimit": 50,
                "description": "A description about some business",
                "categories": ["Category 1"]
            }
    }
]

I have a struct that looks like this 我有一个看起来像这样的结构

struct Service: Hashable, Codable, Identifiable { 

    var id: Int
    var businessName: String
    var businessTelephone: String
    var businessEmail: String
    var businessWebsite: String
    var businessLocation: Array<Any>
    var travelLimit: Int
    var description: String
    var categories: [Category]

    enum Category: String, CaseIterable, Hashable, Codable {

        case category1 = "Category 1"
        case category2 = "Category 2"

    }

}

However, I get the following errors 但是,出现以下错误

Type 'Service' does not conform to protocol 'Decodable'
Type 'Service' does not conform to protocol 'Encodable'
Type 'Service' does not conform to protocol 'Equatable'
Type 'Service' does not conform to protocol 'Hashable'

Codable can't have Any , plus businessLocation is a dictionary not an array, so Replace Codable不能具有Any ,加上businessLocation是一个字典,而不是一个数组,因此Replace

var businessLocation: Array<Any>

with

var businessLocation:[String:String]

OR 要么

Models 楷模

// MARK: - Element
struct Root: Codable {
    let id, name: String
    let items: [Item]
}

// MARK: - Item
struct Service: Codable {
    let id: Int
    let businessName, businessTelephone, businessEmail, businessWebsite: String
    let businessLocation: BusinessLocation
    let travelLimit: Int
    let itemDescription: String
    let categories: [String]

    enum CodingKeys: String, CodingKey {
        case id, businessName, businessTelephone, businessEmail, businessWebsite, businessLocation, travelLimit
        case itemDescription = "description"
        case categories
    }
}

// MARK: - BusinessLocation
struct BusinessLocation: Codable {
    let latitude, longitude: String
}

Decode 解码

do {
    let res = try JSONDecoder().decode([Root].self, from: data)
    print(res)
}
catch {
    print(error)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Argo:类型不符合“可解码”协议 - Argo: Type does not conform to protocol 'Decodable' Swift 4.2:类型“ T”不符合协议“可解码” - Swift 4.2 : Type 'T' does not conform to protocol 'Decodable' 如果我在 swift 的结构中使用协议类型,我的结构不符合协议“可解码”/“可编码” - My structure does not conform to protocol 'Decodable' / 'Encodable' if I use protocol type in my structure in swift 类型“”不符合协议“可编码” - Type ' ' does not conform to protocol 'Encodable' 类型[String:String]不符合协议“ AnyObject” - type [String: String] does not conform to protocol 'AnyObject' 类型“ AreaData”不符合协议“可编码” - Type 'AreaData' does not conform to protocol 'Encodable' 致命错误:字典 <String, Any> 不符合Decodable,因为Any不符合Decodable - Fatal error: Dictionary<String, Any> does not conform to Decodable because Any does not conform to Decodable 类型“字符串”不符合协议“ NSCopying”-数组swift json错误 - Type 'String' does not conform to protocol 'NSCopying' - Array swift json Error Swift,ObjectMapper:类型“用户”不符合协议“可映射” - Swift, ObjectMapper: Type 'User' does not conform to protocol 'Mappable' 为什么这个模型不符合 Decodable? (一个多态的 JSON 圣诞故事) - Why does this model not conform to Decodable? (a polymorphic JSON Christmas tale)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM