Swift 4中的通用解码器

时间:2018-03-14 10:49:14

标签: swift generics swift4 codable

目前我正在使用此代码处理某些数据的解码:

private func parseJSON(_ data: Data) throws -> [ParsedType] 
{
    let decoder = JSONDecoder()
    let parsed = try decoder.decode([ParsedType].self, from: data)
    return parsed
}

private func parsePlist(_ data: Data) throws -> [ParsedType] 
{
    let decoder = PropertyListDecoder()
    let parsed = try decoder.decode([ParsedType].self, from: data)
    return parsed
}

有没有办法创建一个将所有重复代码绑定在一起的通用方法?

private func parse(_ data: Data, using decoder: /*Something*/) throws -> [ParsedType]
{
    let parsed = try decoder.decode([ParsedType].self, from: data)
    return parsed
}

2 个答案:

答案 0 :(得分:2)

如果您查看JSONEncoderPropertyListDecoder的swift stdlib,您会看到它们共享一个方法

func decode<T: Decodable >(_ type: T.Type, from data: Data) throws -> T

因此,您可以创建一个具有所述方法的协议,并将两个解码器都符合它:

protocol DecoderType {
    func decode<T: Decodable >(_ type: T.Type, from data: Data) throws -> T
}

extension JSONDecoder: DecoderType { }
extension PropertyListDecoder: DecoderType { }

并像这样创建通用解析函数:

func parseData(_ data: Data, with decoder: DecoderType) throws ->  [ParsedType] {
    return try decoder.decode([ParsedType].self, from: data)
}

答案 1 :(得分:0)

Decodable + Generic.swift

import Foundation

// Generic decode method for Decodable
func decode<T: Decodable>(data: Data) throws -> T {
    let decoder = JSONDecoder()
    return try decoder.decode(T.self, from: data)
}

Decodable + GenericTests.swift

import XCTest
@testable import YourProject

class Decodable_EncodableTests: XCTestCase {
    func testDecodableEncodable() {
        struct User: Decodable, Encodable {
            let name: String
            let sex: String
        }
        let json = """
        {
            "name": "Ronaldo",
            "sex": "Female"
        }
        """.data(using: .utf8)!

        do {
            // When
            let name = "Ronaldo"
            let sex = "Female"
            let user: User = try decode(data: json)

            // Then
            XCTAssertEqual(user.name, name)
            XCTAssertEqual(user.sex, sex)

        } catch let error {
            XCTFail(error.localizedDescription)
        }
    }
}