代码之家  ›  专栏  ›  技术社区  ›  swiftPunk Alexander

为什么我的CustomType即使符合Decodable也不能符合它?

  •  0
  • swiftPunk Alexander  · 技术社区  · 4 年前

    我有一个符合Decodable的CustomType,当我想用它作为我函数所需的值时,Xcode抱怨CustomType不符合Decodable!如果我显式地使CustomType确认发生,我也做了,但它没有解决问题!我错过了什么?

    错误:

    “类型”自定义类型。类型“”不能与“可解码”一致

    let stringOfJSON: String = """
    { "name": "SwiftPunk", "age": 35 }
    """
    
    let dataOfJSON: Data? = stringOfJSON.data(using: String.Encoding.utf8)
    
    struct CustomType: Decodable {
        
        enum Codingkeys: String, CodingKey {
            case name, age
        }
        
        var name: String
        var age: Int
        
        init(from decoder: Decoder) throws {
            let container = try decoder.container(keyedBy: Codingkeys.self)
            name = try container.decode(String.self, forKey: .name)
            age = try container.decode(Int.self, forKey: .age)
        }
        
        
    }
    
    func decoderFunction<T: Decodable>(dataOfJSON: Data?, customType: T, decodedValue: (T) -> Void) {
        
        if let unwrappedDataOfJSON: Data = dataOfJSON {
            
            let dataJSONDecoder: JSONDecoder = JSONDecoder()
            
            do {
                let value: T = try dataJSONDecoder.decode(T.self, from: unwrappedDataOfJSON)
                decodedValue(value)
            } catch {
                print("The Data could not be decoded!")
            }
        }
    }
    

    用例:

    decoderFunction(dataOfJSON: dataOfJSON, customType: CustomType.self, decodedValue: { value in
     
    })
    
    3 回复  |  直到 4 年前
        1
  •  2
  •   Sweeper    4 年前

    类型 customType 参数不正确。应该是 元类型 T ( T.Type ),不是 .

    func decoderFunction<T: Decodable>(dataOfJSON: Data?, customType: T.Type, decodedValue: (T) -> Void) {
        ...
    }
    

    该方法应为类型为的值 T Decodable ,但你给了它一个类型的值 CustomType.Type (但请注意 CustomType

    自定义类型 参数。 T

    func decoderFunction<T: Decodable>(dataOfJSON: Data?, decodedValue: (T) -> Void) {
        ...
    }
    
    decoderFunction(dataOfJSON: dataOfJSON, decodedValue: { (value: CustomType) in
     
    })
    
        2
  •  1
  •   Rob C    4 年前

    将函数签名更改为:

    func decoderFunction<T: Decodable>(
        dataOfJSON: Data?, 
        customType: T.Type, 
        decodedValue: (T) -> Void)
    

    您希望传入的类型为 T 没有价值 T

        3
  •  1
  •   vadian    4 年前

    进一步使用 Result 键入以处理潜在错误,并将JSON字符串传递给函数,而不是可选的 Data

    let stringOfJSON: String = """
    { "name": "SwiftPunk", "age": 35 }
    """
    
    struct CustomType: Decodable {
        let name: String
        let age: Int
    }
    
    func decoderFunction<T: Decodable>(json: String, decodedValue: (Result<T,Error>) -> Void) {
        decodedValue(Result {
            try JSONDecoder().decode(T.self, from: Data(json.utf8))
        })
    }
    

    decoderFunction(json: stringOfJSON) { (result : Result<CustomType,Error>) in
        
        switch result {
            case .success(let value): print(value)
            case .failure(let error): print(error)
        }
    }