Swift 4可解码var可以是Bool或自定义类型

时间:2018-08-14 09:00:38

标签: json swift4 decodable

我一直为此苦苦挣扎。我有一个从API调用中获取的JSON,但它具有一个密钥,该密钥可以为false或返回true的值。

像这样:

{
  "id": 550,
  "favorite": true,
  "rated": {
    "value": 8
  },
  "watchlist": false
}

或者这个:

{
  "id": 550,
  "favorite": true,
  "rated": false,
  "watchlist": false
}

我试图这样解码:

struct AccountState: Decodable {
    var id: Int?
    var favorite: Bool?
    var rated: CustomValue
    var watchlist: Bool?
}

struct RatingValue: Decodable {
    var value: Double?
}

enum CustomValue: Decodable {
    case bool(Bool)
    case rating(RatingValue)

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()

        if let bool = try? container.decode(Bool.self) {
            self = .bool(bool)
        } else if let rating = try? container.decode(RatingValue.self) {
            self = .rating(rating)
        } else {
            let context = DecodingError.Context(codingPath: container.codingPath, debugDescription: "Unknown type")
        throw DecodingError.dataCorrupted(context)
        }
    }
}

在ViewController中:

func dowloadAndDecodeData() {
 let url...
 ...

 let decoder = JSONDecoder()
            decoder.keyDecodingStrategy = .convertFromSnakeCase
            guard let accountState = try? decoder.decode(AccountState.self, from: data) else {
                print("error")
                return
            }
 print(accountState)
}

在控制台中,我可以看到JSON内容已正确解析(如果存在,则返回false或value)。

问题是:如何从代码中访问此值?由于“等级”的类型为“ CustomValue”,因此我不能像平时那样使用accountState.rated.value

2 个答案:

答案 0 :(得分:0)

要访问类似CustomValue(具有关联值的enum案例)之类的内容,您需要使用switch-case或if-case。

    switch accountState.rated {
    case .rating(let ratingValue):
        print(ratingValue.value)
    case .bool(let boolValue):
        print(boolValue);
    }

否则,您可以为CustomValue定义一个简单的扩展名:

extension CustomValue {
    var value: Double? {
        switch self {
        case .rating(let ratingValue):
            return ratingValue.value
        case .bool: //### in case .bool, return nil
            return nil
        }
    }
}

您可以通过以下方式简单地访问它:

    if let value = accountState.rated.value {
        print(value)
    }

答案 1 :(得分:0)

我遇到了同样的问题,我做了一个名为Any的财产。所以我可以将其转换为所需的数据类型。

在这里检查我的答案。 How to use Any in Codable Type

我还没有添加布尔类型,但是它将100%解决您的问题,如果您需要进一步的帮助,请告诉我

在我的CustomValue类中添加以下属性之前,请先阅读我的答案。

 var any:Any{
        get{
            switch self {
            case .bool(let value):
                return value
            }
        }
    }