Swift 4 JSONDecoder解码协议类型

时间:2017-10-14 03:20:12

标签: ios swift decodable

以下代码无法编译:

public protocol Foo: Decodable {
  var message: String { get }
}
struct Bar: Foo {
  let message: String
}

// the type conforming Foo protocol is passed from somewhere
let type: Foo.Type = Bar.self
decode(type: type, from: Data())

func decode<T>(type: T.Type, from data: Data) Where T: Decodable {
  let decoder = JSONDecoder()
  try! decoder.decode(type, from: data)
}

它会抛出错误: Cannot invoke 'decode' with an argument list of type '(Foo.Type, from: Data)'

你们有什么想法吗?

2 个答案:

答案 0 :(得分:3)

您可以这样使用:

public protocol Foo: Decodable {
    var message: String { get }
}
struct Bar: Foo {
    let message: String
}

class ViewController: UIViewController {
    let bar = """
        {"message": "Sample message"}
    """

    override func viewDidLoad() {
        super.viewDidLoad()
        let type = Bar.self
        decode(type: type, from: bar.data(using: .utf8)!)
    }

    func decode<Foo>(type: Foo.Type, from data: Data) where Foo: Decodable {
        let decoder = JSONDecoder()
        let parsedData = try! decoder.decode(type, from: data)
        print(parsedData)
    }
}

答案 1 :(得分:2)

您应该在Codable上使用Bar代替:

protocol Foo {
    var message: String { get }
}
struct Bar: Foo, Codable {
    let message: String
}

用法:

let bar = Bar(message: "Just a message")
if let data = try? JSONEncoder().encode(bar) {
    print(String(data:data, encoding:.utf8) ?? "") // {"message":"Just a message"}\n"// lets decode it
    if let decodedBar = try? JSONDecoder().decode(Bar.self, from: data) {
        print(decodedBar.message) //"Just a message\n"
     }
}